1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package zou;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import javax.swing.ComboBoxEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.EventListenerList;
import javax.swing.text.NumberFormatter;
//import org.jdesktop.swingx.JXPanel;
/**
*
* @author fabriceb
*/
//public class FontChooser extends JXPanel {
public class FontChooser extends javax.swing.JPanel {
private static final long serialVersionUID = 1l;
/**
* The user selected font.
*/
public static final String SELECTED_FONT_PROPERTY = "selectedFont";
/**
* Default font sizes.
*/
private static final int[] DEFAULT_SIZE = {8, 9, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72};
/**
* Editing flag.
*/
private boolean isEditing;
/**
* Creates instance with default font.
*/
public FontChooser() {
this(null);
}
/**
* Creates instance with given font.
* @param font The font.
*/
public FontChooser(Font font) {
super();
initComponents();
//
isEditing = true;
String[] families = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
DefaultComboBoxModel familyModel = (DefaultComboBoxModel) familyCombo.getModel();
for (String family : families) {
familyModel.addElement(family);
}
DefaultComboBoxModel sizeModel = (DefaultComboBoxModel) sizeCombo.getModel();
for (int size : DEFAULT_SIZE) {
sizeModel.addElement(size);
}
isEditing = false;
//
setSelectedFont(font);
}
/**
* Sets the selected <code>Font</code>.
* @param value The new value.
* <br/>If <code>null</code> uses the default font of this component instead.
* @see #getSelectedFont()
*/
public void setSelectedFont(Font value) {
if (value == null) {
value = getFont();
}
putClientProperty(SELECTED_FONT_PROPERTY, value);
isEditing = true;
familyCombo.setSelectedItem(value.getFamily());
sizeCombo.setSelectedItem(value.getSize());
boldButton.setSelected(value.isBold());
italicButton.setSelected(value.isItalic());
previewArea.setFont(value);
isEditing = false;
}
/**
* Gets the selected <code>Font</code>.
* @return A <code>Font</code> instance, never <code>null</code>.
* @see #setSelectedFont(java.awt.Font)
*/
public Font getSelectedFont() {
return (Font)getClientProperty(SELECTED_FONT_PROPERTY);
}
protected void applyNewFont() {
String family = (String) familyCombo.getSelectedItem();
int size = ((Number) sizeCombo.getSelectedItem()).intValue();
int style = boldButton.isSelected() ? Font.BOLD : Font.PLAIN;
style = italicButton.isSelected() ? style | Font.ITALIC : style;
Font font = new Font(family, style, size);
putClientProperty(SELECTED_FONT_PROPERTY, font);
previewArea.setFont(font);
}
/**
* Self-test main.
* @param args Arguments from the command line.
*/
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new FontChooser(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JOptionPane.showMessageDialog(null, new FontChooser());
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* A combo box editor that only accepts integers.
* @author fabriceb
*/
private class SizeComboBoxEditor implements ComboBoxEditor, PropertyChangeListener {
private JFormattedTextField delegated = new JFormattedTextField(new NumberFormatter(NumberFormat.getIntegerInstance()));
private EventListenerList listenerList = new EventListenerList();
/**
* Creates a new instance.
*/
public SizeComboBoxEditor() {
delegated.addPropertyChangeListener("value", this);
delegated.setBorder(null);
}
/**
* {@inheritDoc}
*/
@Override
public Component getEditorComponent() {
return delegated;
}
/**
* {@inheritDoc}
*/
@Override
public void selectAll() {
delegated.selectAll();
delegated.requestFocus();
}
/**
* {@inheritDoc}
*/
@Override
public void setItem(Object anObject) {
if (delegated.getValue() == null || !delegated.getValue().equals(anObject)) {
delegated.setValue(anObject);
}
}
/**
* {@inheritDoc}
*/
@Override
public Object getItem() {
return delegated.getValue();
}
/**
* {@inheritDoc}
*/
@Override
public void addActionListener(ActionListener l) {
listenerList.add(ActionListener.class, l);
}
/**
* {@inheritDoc}
*/
@Override
public void removeActionListener(ActionListener l) {
listenerList.remove(ActionListener.class, l);
}
protected void fireActionEvent(Integer value) {
Object listeners[] = listenerList.getListenerList();
ActionEvent actionEvent = null;
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ActionListener.class) {
// Lazily create the event.
if (actionEvent == null) {
actionEvent = new ActionEvent(delegated, ActionEvent.ACTION_PERFORMED, String.valueOf(value));
}
((ActionListener) listeners[i + 1]).actionPerformed(actionEvent);
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void propertyChange(PropertyChangeEvent event) {
if (!isEditing) {
Object value = delegated.getValue();
if (value == null) {
return;
}
int val = ((Number) value).intValue();
System.out.println("Should forward " + value);
fireActionEvent(val);
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
familyLabel = new JLabel();
previewScroll = new javax.swing.JScrollPane();
previewArea = new javax.swing.JTextArea();
familyCombo = new javax.swing.JComboBox();
previewSeparator = new JLabel();
boldButton = new javax.swing.JToggleButton();
italicButton = new javax.swing.JToggleButton();
sizeLabel = new JLabel();
sizeCombo = new javax.swing.JComboBox();
styleLabel = new JLabel();
setName("Form"); // NOI18N
// org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.spc.ofp.seapodym.view.SeapodymViewApp.class).getContext().getResourceMap(FontChooser.class);
// familyLabel.setText(resourceMap.getString("familyLabel.text")); // NOI18N
// familyLabel.setName("familyLabel"); // NOI18N
previewScroll.setName("previewScroll"); // NOI18N
previewArea.setColumns(20);
previewArea.setEditable(false);
previewArea.setLineWrap(true);
previewArea.setRows(5);
// previewArea.setText(resourceMap.getString("previewArea.text")); // NOI18N
previewArea.setName("previewArea"); // NOI18N
previewScroll.setViewportView(previewArea);
familyCombo.setModel(new javax.swing.DefaultComboBoxModel());
familyCombo.setName("familyCombo"); // NOI18N
familyCombo.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
familyComboItemStateChanged(evt);
}
});
// previewSeparator.setTitle(resourceMap.getString("previewSeparator.title")); // NOI18N
// previewSeparator.setName("previewSeparator"); // NOI18N
// boldButton.setText(resourceMap.getString("boldButton.text")); // NOI18N
// boldButton.setToolTipText(resourceMap.getString("boldButton.toolTipText")); // NOI18N
boldButton.setName("boldButton"); // NOI18N
boldButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
boldButtonActionPerformed(evt);
}
});
// italicButton.setText(resourceMap.getString("italicButton.text")); // NOI18N
// italicButton.setToolTipText(resourceMap.getString("italicButton.toolTipText")); // NOI18N
italicButton.setName("italicButton"); // NOI18N
italicButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
italicButtonActionPerformed(evt);
}
});
// sizeLabel.setText(resourceMap.getString("sizeLabel.text")); // NOI18N
// sizeLabel.setName("sizeLabel"); // NOI18N
sizeCombo.setEditable(true);
sizeCombo.setModel(new javax.swing.DefaultComboBoxModel());
sizeCombo.setEditor(new SizeComboBoxEditor());
sizeCombo.setName("sizeCombo"); // NOI18N
sizeCombo.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
sizeComboItemStateChanged(evt);
}
});
// styleLabel.setText(resourceMap.getString("styleLabel.text")); // NOI18N
// styleLabel.setName("styleLabel"); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(previewScroll, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(familyLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(familyCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sizeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sizeCombo, 0, 73, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(styleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(boldButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(italicButton))
.addComponent(previewSeparator, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(familyLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sizeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(familyCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sizeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(boldButton, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(styleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(italicButton, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(previewSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(previewScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>
private void familyComboItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
if (!isEditing && evt.getStateChange() == ItemEvent.SELECTED) {
applyNewFont();
}
}
private void sizeComboItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
if (!isEditing && evt.getStateChange() == ItemEvent.SELECTED) {
Object value = sizeCombo.getSelectedItem();
System.out.println("Received " + value + "\t" + value.getClass());
if ((value instanceof Integer) || (value instanceof Long)) {
int size = ((Number) value).intValue();
DefaultComboBoxModel sizeModel = (DefaultComboBoxModel) sizeCombo.getModel();
int sizeCount = sizeModel.getSize();
for (int i = 0; i <
sizeCount; i++) {
int val = ((Number) sizeModel.getElementAt(i)).intValue();
// Value already in combo.
if (size == val) {
break;
}
// Insert before current value.
else if (val > size) {
sizeModel.insertElementAt(size, i);
break;
}
// Add at end.
else if (i == sizeCount - 1) {
sizeModel.addElement(size);
break;
}
}
applyNewFont();
}
}
}
private void boldButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (!isEditing) {
applyNewFont();
}
}
private void italicButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (!isEditing) {
applyNewFont();
}
}
// Variables declaration - do not modify
private javax.swing.JToggleButton boldButton;
private javax.swing.JComboBox familyCombo;
private JLabel familyLabel;
private javax.swing.JToggleButton italicButton;
private javax.swing.JTextArea previewArea;
private javax.swing.JScrollPane previewScroll;
private JLabel previewSeparator;
private javax.swing.JComboBox sizeCombo;
private JLabel sizeLabel;
private JLabel styleLabel;
// End of variables declaration
} |
Partager