001: /*
002: * HexColorChooserPanel.java
003: *
004: * Created on May 7, 2007, 10:17 AM
005: */
006:
007: package it.businesslogic.ireport.gui.sheet;
008:
009: import java.awt.Color;
010: import java.awt.Component;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import java.awt.event.FocusEvent;
014: import java.awt.event.FocusListener;
015: import java.awt.event.KeyEvent;
016: import java.awt.event.KeyListener;
017: import javax.swing.Icon;
018: import javax.swing.JColorChooser;
019: import javax.swing.JDialog;
020: import javax.swing.colorchooser.AbstractColorChooserPanel;
021: import javax.swing.event.DocumentEvent;
022: import javax.swing.event.DocumentListener;
023: import javax.swing.text.AbstractDocument;
024: import javax.swing.text.AttributeSet;
025: import javax.swing.text.BadLocationException;
026: import javax.swing.text.DocumentFilter;
027:
028: /**
029: *
030: * @author gtoffoli
031: */
032: public class HexColorChooserPanel extends
033: javax.swing.colorchooser.AbstractColorChooserPanel {
034:
035: private JDialog dialog = null;
036: private Color selectedColor = null;
037: private boolean init = false;
038:
039: /** Creates new form HexColorChooserPanel */
040: public HexColorChooserPanel() {
041: initComponents();
042:
043: jTextField1.setDocument(new MaskedPlainDocument(
044: MaskedPlainDocument.COLOR_MASK));
045:
046: /*
047: ((AbstractDocument)jTextField1.getDocument()).setDocumentFilter(new DocumentFilter(){
048:
049: // This method is called when characters are inserted into the document
050: public void insertString(DocumentFilter.FilterBypass fb, int offset, String str,
051: AttributeSet attr) throws BadLocationException {
052: replace(fb, offset, 0, str, attr);
053: }
054:
055: // This method is called when characters in the document are replace with other characters
056: public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
057: String str, AttributeSet attrs) throws BadLocationException {
058: int newLength = fb.getDocument().getLength()-length+str.length();
059: if (newLength <= 6 || ( newLength ==7 && fb.getDocument().getText(0,1).startsWith("#"))) {
060: fb.replace(offset, length, str, attrs);
061: } else {
062: throw new BadLocationException("New characters exceeds max size of document", offset);
063: }
064: }
065: } );
066: */
067:
068: /*
069: jTextField1.addKeyListener(new KeyListener() {
070: public void keyPressed(KeyEvent e) {
071:
072: if (e.getKeyCode() == e.VK_ENTER)
073: {
074: if (jTextField1.getText().length() >= 6)
075: {
076: this.updateColor();
077: }
078: }
079: }
080: public void keyReleased(KeyEvent e) {
081: }
082: public void keyTyped(KeyEvent e) {
083: }
084: });
085: */
086:
087: jTextField1.getDocument().addDocumentListener(
088: new DocumentListener() {
089: public void changedUpdate(DocumentEvent e) {
090: if (jTextField1.getText().length() >= 6) {
091: updateColor();
092: }
093: }
094:
095: public void insertUpdate(DocumentEvent e) {
096: if (jTextField1.getText().length() >= 6) {
097: updateColor();
098: }
099: }
100:
101: public void removeUpdate(DocumentEvent e) {
102: }
103: });
104:
105: jTextField1.addFocusListener(new FocusListener() {
106: public void focusGained(FocusEvent e) {
107: }
108:
109: public void focusLost(FocusEvent e) {
110: updateColor();
111: }
112: });
113: }
114:
115: public void updateColor() {
116: if (isInit())
117: return;
118: Color c = parseColorString(jTextField1.getText());
119: if (c != null) {
120: setInit(true);
121: getColorSelectionModel().setSelectedColor(c);
122:
123: setInit(false);
124: }
125: }
126:
127: public static Color parseColorString(String newValue) {
128: if (newValue == null)
129: return null;
130:
131: newValue = newValue.trim();
132: // Try to create the color from a string...
133: java.awt.Color c = null;
134: if (newValue.startsWith("#")) {
135: newValue = newValue.substring(1);
136: }
137: if (newValue.length() == 6) {
138: try {
139: int hr = Integer.parseInt(newValue.substring(0, 2), 16);
140: int hg = Integer.parseInt(newValue.substring(2, 4), 16);
141: int hb = Integer.parseInt(newValue.substring(4, 6), 16);
142: c = new Color(hr, hg, hb);
143: return c;
144: } catch (Exception ex) {
145:
146: }
147: }
148:
149: return null;
150:
151: }
152:
153: /** This method is called from within the constructor to
154: * initialize the form.
155: * WARNING: Do NOT modify this code. The content of this method is
156: * always regenerated by the Form Editor.
157: */
158: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
159: private void initComponents() {
160: java.awt.GridBagConstraints gridBagConstraints;
161:
162: jLabel1 = new javax.swing.JLabel();
163: jTextField1 = new javax.swing.JTextField();
164: jPanel1 = new javax.swing.JPanel();
165:
166: setLayout(new java.awt.GridBagLayout());
167:
168: jLabel1.setText("Hex 0x");
169: gridBagConstraints = new java.awt.GridBagConstraints();
170: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 0);
171: add(jLabel1, gridBagConstraints);
172:
173: jTextField1.setText("000000");
174: jTextField1.setMinimumSize(new java.awt.Dimension(100, 19));
175: jTextField1.setPreferredSize(new java.awt.Dimension(100, 19));
176: gridBagConstraints = new java.awt.GridBagConstraints();
177: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
178: gridBagConstraints.weightx = 1.0;
179: gridBagConstraints.insets = new java.awt.Insets(4, 2, 4, 4);
180: add(jTextField1, gridBagConstraints);
181:
182: jPanel1.setLayout(new java.awt.BorderLayout());
183:
184: gridBagConstraints = new java.awt.GridBagConstraints();
185: gridBagConstraints.gridx = 0;
186: gridBagConstraints.weighty = 1.0;
187: add(jPanel1, gridBagConstraints);
188:
189: }// </editor-fold>//GEN-END:initComponents
190:
191: // Variables declaration - do not modify//GEN-BEGIN:variables
192: private javax.swing.JLabel jLabel1;
193: private javax.swing.JPanel jPanel1;
194: private javax.swing.JTextField jTextField1;
195:
196: // End of variables declaration//GEN-END:variables
197:
198: public static String getEncodedColor(java.awt.Color c) {
199: String nums = "0123456789ABCDEF";
200: String s = "#";
201: s += nums.charAt(c.getRed() / 16);
202: s += nums.charAt(c.getRed() % 16);
203: s += nums.charAt(c.getGreen() / 16);
204: s += nums.charAt(c.getGreen() % 16);
205: s += nums.charAt(c.getBlue() / 16);
206: s += nums.charAt(c.getBlue() % 16);
207: return s;
208: }
209:
210: public void updateChooser() {
211:
212: if (isInit())
213: return;
214: Color c = this .getColorFromModel();
215: if (c != null) {
216: boolean oldValue = isInit();
217: setInit(true);
218: try {
219:
220: jTextField1.setText(getEncodedColor(c));
221: } finally {
222: setInit(oldValue);
223: }
224: }
225: }
226:
227: protected void buildChooser() {
228: }
229:
230: public String getDisplayName() {
231: return "Hex Value";
232: }
233:
234: public Icon getSmallDisplayIcon() {
235: return null;
236: }
237:
238: public Icon getLargeDisplayIcon() {
239: return null;
240: }
241:
242: public static Color showDialog(Component c, String title,
243: Color defColor) {
244: final JColorChooser jcc = new JColorChooser();
245: final HexColorChooserPanel hcp = new HexColorChooserPanel();
246: AbstractColorChooserPanel[] current_panels = jcc
247: .getChooserPanels();
248: AbstractColorChooserPanel[] panels = new AbstractColorChooserPanel[current_panels.length + 1];
249: int i = 0;
250: for (; i < current_panels.length; ++i) {
251: panels[i] = current_panels[i];
252: }
253: panels[i] = hcp;
254: jcc.setChooserPanels(panels);
255:
256: JDialog theDialog = jcc.createDialog(c, title, true, jcc,
257: new ActionListener() {
258: public void actionPerformed(ActionEvent e) {
259: hcp.setSelectedColor(jcc.getColor());
260: hcp.getDialog().setVisible(false);
261: hcp.getDialog().dispose();
262: }
263: }, new ActionListener() {
264: public void actionPerformed(ActionEvent e) {
265: hcp.setSelectedColor(null);
266: hcp.getDialog().setVisible(false);
267: hcp.getDialog().dispose();
268: }
269: });
270: hcp.setDialog(theDialog);
271: theDialog.setVisible(true);
272:
273: return hcp.getSelectedColor();
274: }
275:
276: public Color getSelectedColor() {
277: return selectedColor;
278: }
279:
280: public void setSelectedColor(Color selectedColor) {
281: this .selectedColor = selectedColor;
282: }
283:
284: private JDialog getDialog() {
285: return dialog;
286: }
287:
288: private void setDialog(JDialog dialog) {
289: this .dialog = dialog;
290: }
291:
292: public boolean isInit() {
293: return init;
294: }
295:
296: public void setInit(boolean init) {
297: this.init = init;
298: }
299:
300: }
|