< prev index next >

src/java.desktop/share/classes/com/sun/beans/editors/ColorEditor.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.beans.editors;
  27 
  28 import java.awt.*;
  29 import java.beans.*;
  30 
  31 public class ColorEditor extends Panel implements PropertyEditor {
  32     private static final long serialVersionUID = 1781257185164716054L;
  33 

  34     public ColorEditor() {
  35         setLayout(null);
  36 
  37         ourWidth = hPad;
  38 
  39         // Create a sample color block bordered in black
  40         Panel p = new Panel();
  41         p.setLayout(null);
  42         p.setBackground(Color.black);
  43         sample = new Canvas();
  44         p.add(sample);
  45         sample.reshape(2, 2, sampleWidth, sampleHeight);
  46         add(p);
  47         p.reshape(ourWidth, 2, sampleWidth+4, sampleHeight+4);
  48         ourWidth += sampleWidth + 4 + hPad;
  49 
  50         text = new TextField("", 14);
  51         add(text);
  52         text.reshape(ourWidth,0,100,30);
  53         ourWidth += 100 + hPad;
  54 
  55         choser = new Choice();
  56         int active = 0;
  57         for (int i = 0; i < colorNames.length; i++) {
  58             choser.addItem(colorNames[i]);
  59         }
  60         add(choser);
  61         choser.reshape(ourWidth,0,100,30);
  62         ourWidth += 100 + hPad;
  63 
  64         resize(ourWidth,40);
  65     }
  66 
  67     public void setValue(Object o) {
  68         Color c = (Color)o;
  69         changeColor(c);
  70     }
  71 

  72     public Dimension preferredSize() {
  73         return new Dimension(ourWidth, 40);
  74     }
  75 

  76     public boolean keyUp(Event e, int key) {
  77         if (e.target == text) {
  78             try {
  79                 setAsText(text.getText());
  80             } catch (IllegalArgumentException ex) {
  81                 // Quietly ignore.
  82             }
  83         }
  84         return (false);
  85     }
  86 
  87     public void setAsText(String s) throws java.lang.IllegalArgumentException {
  88         if (s == null) {
  89             changeColor(null);
  90             return;
  91         }
  92         int c1 = s.indexOf(',');
  93         int c2 = s.indexOf(',', c1+1);
  94         if (c1 < 0 || c2 < 0) {
  95             // Invalid string.
  96             throw new IllegalArgumentException(s);
  97         }
  98         try {
  99             int r = Integer.parseInt(s.substring(0,c1));
 100             int g = Integer.parseInt(s.substring(c1+1, c2));
 101             int b = Integer.parseInt(s.substring(c2+1));
 102             Color c = new Color(r,g,b);
 103             changeColor(c);
 104         } catch (Exception ex) {
 105             throw new IllegalArgumentException(s);
 106         }
 107 
 108     }
 109 

 110     public boolean action(Event e, Object arg) {
 111         if (e.target == choser) {
 112             changeColor(colors[choser.getSelectedIndex()]);
 113         }
 114         return false;
 115     }
 116 
 117     public String getJavaInitializationString() {
 118         return (this.color != null)
 119                 ? "new java.awt.Color(" + this.color.getRGB() + ",true)"
 120                 : "null";
 121     }
 122 
 123 
 124     private void changeColor(Color c) {
 125 
 126         if (c == null) {
 127             this.color = null;
 128             this.text.setText("");
 129             return;


   1 /*
   2  * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.beans.editors;
  27 
  28 import java.awt.*;
  29 import java.beans.*;
  30 
  31 public class ColorEditor extends Panel implements PropertyEditor {
  32     private static final long serialVersionUID = 1781257185164716054L;
  33 
  34     @SuppressWarnings("deprecation")
  35     public ColorEditor() {
  36         setLayout(null);
  37 
  38         ourWidth = hPad;
  39 
  40         // Create a sample color block bordered in black
  41         Panel p = new Panel();
  42         p.setLayout(null);
  43         p.setBackground(Color.black);
  44         sample = new Canvas();
  45         p.add(sample);
  46         sample.reshape(2, 2, sampleWidth, sampleHeight);
  47         add(p);
  48         p.reshape(ourWidth, 2, sampleWidth+4, sampleHeight+4);
  49         ourWidth += sampleWidth + 4 + hPad;
  50 
  51         text = new TextField("", 14);
  52         add(text);
  53         text.reshape(ourWidth,0,100,30);
  54         ourWidth += 100 + hPad;
  55 
  56         choser = new Choice();
  57         int active = 0;
  58         for (int i = 0; i < colorNames.length; i++) {
  59             choser.addItem(colorNames[i]);
  60         }
  61         add(choser);
  62         choser.reshape(ourWidth,0,100,30);
  63         ourWidth += 100 + hPad;
  64 
  65         resize(ourWidth,40);
  66     }
  67 
  68     public void setValue(Object o) {
  69         Color c = (Color)o;
  70         changeColor(c);
  71     }
  72 
  73     @SuppressWarnings("deprecation")
  74     public Dimension preferredSize() {
  75         return new Dimension(ourWidth, 40);
  76     }
  77 
  78     @SuppressWarnings("deprecation")
  79     public boolean keyUp(Event e, int key) {
  80         if (e.target == text) {
  81             try {
  82                 setAsText(text.getText());
  83             } catch (IllegalArgumentException ex) {
  84                 // Quietly ignore.
  85             }
  86         }
  87         return (false);
  88     }
  89 
  90     public void setAsText(String s) throws java.lang.IllegalArgumentException {
  91         if (s == null) {
  92             changeColor(null);
  93             return;
  94         }
  95         int c1 = s.indexOf(',');
  96         int c2 = s.indexOf(',', c1+1);
  97         if (c1 < 0 || c2 < 0) {
  98             // Invalid string.
  99             throw new IllegalArgumentException(s);
 100         }
 101         try {
 102             int r = Integer.parseInt(s.substring(0,c1));
 103             int g = Integer.parseInt(s.substring(c1+1, c2));
 104             int b = Integer.parseInt(s.substring(c2+1));
 105             Color c = new Color(r,g,b);
 106             changeColor(c);
 107         } catch (Exception ex) {
 108             throw new IllegalArgumentException(s);
 109         }
 110 
 111     }
 112 
 113     @SuppressWarnings("deprecation")
 114     public boolean action(Event e, Object arg) {
 115         if (e.target == choser) {
 116             changeColor(colors[choser.getSelectedIndex()]);
 117         }
 118         return false;
 119     }
 120 
 121     public String getJavaInitializationString() {
 122         return (this.color != null)
 123                 ? "new java.awt.Color(" + this.color.getRGB() + ",true)"
 124                 : "null";
 125     }
 126 
 127 
 128     private void changeColor(Color c) {
 129 
 130         if (c == null) {
 131             this.color = null;
 132             this.text.setText("");
 133             return;


< prev index next >