KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > propedit > LabelValuePropertyEditor


1 package net.suberic.util.gui.propedit;
2 import javax.swing.*;
3 import java.awt.Dimension JavaDoc;
4 import java.util.logging.Logger JavaDoc;
5
6 /**
7  * This provides the base class for a simple, label-on-the-left
8  * value-on-the-right property editor.
9  */

10 public abstract class LabelValuePropertyEditor extends SwingPropertyEditor {
11   // the label component. this is used for a default implementation
12
// of the sizing code we have below.
13
protected java.awt.Container JavaDoc labelComponent;
14
15   // the value component. this is used for a default implementation
16
// of the sizing code we have below.
17
protected java.awt.Container JavaDoc valueComponent;
18
19   /**
20    * Run when the PropertyEditor may have changed enabled states.
21    *
22    * This is a default implementation of updateEnabledState. If the
23    * labelComponent and valueComponent attributes are set, it will call
24    * setEnabled on those.
25    *
26    * Subclasses which do not use the default labelComponent and
27    * valueComponent attributes, or which require additional functionality,
28    * should override this method.
29    */

30   protected void updateEditorEnabled() {
31     if (valueComponent != null)
32       valueComponent.setEnabled(isEditorEnabled());
33     if (labelComponent != null)
34       labelComponent.setEnabled(isEditorEnabled());
35   }
36
37   /**
38    * Creates a JLabel for this component.
39    */

40   public JLabel createLabel() {
41     String JavaDoc defaultLabel;
42     int dotIndex = property.lastIndexOf(".");
43     if (dotIndex == -1)
44       defaultLabel = new String JavaDoc(property);
45     else
46       defaultLabel = property.substring(dotIndex+1);
47
48     JLabel returnValue = new JLabel(manager.getProperty(editorTemplate + ".label", defaultLabel));
49
50     return returnValue;
51   }
52
53   /**
54    * Gets the current valueComponent.
55    */

56   public java.awt.Container JavaDoc getValueComponent() {
57     return valueComponent;
58   }
59
60   /**
61    * Gets the current labelComponent.
62    */

63   public java.awt.Container JavaDoc getLabelComponent() {
64     return labelComponent;
65   }
66
67   /**
68    * Gets the minimum size for the labelComponent.
69    */

70   public Dimension JavaDoc getMinimumLabelSize() {
71     if (labelComponent != null) {
72       return labelComponent.getMinimumSize();
73     } else {
74       return new Dimension JavaDoc(0,0);
75     }
76   }
77
78   /**
79    * Gets the minimum size for the valueComponent.
80    */

81   public Dimension JavaDoc getMinimumValueSize() {
82     if (valueComponent != null) {
83       return valueComponent.getMinimumSize();
84     } else {
85       return new Dimension JavaDoc(0,0);
86     }
87   }
88
89   /**
90    * Returns the calculated minimum size for this component.
91    */

92   public Dimension JavaDoc getMinimumTotalSize() {
93     return this.getMinimumSize();
94   }
95
96   /**
97    * Sets the size for the label component and the value component.
98    */

99   public void setSizes(Dimension JavaDoc labelSize, Dimension JavaDoc valueSize) {
100     if (labelComponent != null)
101       labelComponent.setSize(labelSize);
102     if (valueComponent != null)
103       valueComponent.setSize(valueSize);
104   }
105
106   /**
107    * Sets the widths for the label component and the value component.
108    */

109   public void setWidths(int labelWidth, int valueWidth) {
110     if (labelComponent != null)
111       labelComponent.setSize(new Dimension JavaDoc(labelWidth, labelComponent.getSize().height));
112     if (valueComponent != null)
113       valueComponent.setSize(new Dimension JavaDoc(valueWidth, valueComponent.getSize().height));
114   }
115
116   /**
117    * Sets the heights for the label component and the value component.
118    */

119   public void setHeights(int labelHeight, int valueHeight) {
120     if (labelComponent != null)
121       labelComponent.setSize(new Dimension JavaDoc(labelComponent.getSize().width, labelHeight));
122     if (valueComponent != null)
123       valueComponent.setSize(new Dimension JavaDoc(valueComponent.getSize().width, valueHeight));
124   }
125
126   /**
127    * Gets the parent PropertyEditorPane for the given component.
128    */

129   public PropertyEditorPane getPropertyEditorPane() {
130     return getPropertyEditorPane(valueComponent);
131   }
132
133   /**
134    * Returns the display value for this property.
135    */

136   public String JavaDoc getDisplayValue() {
137     java.awt.Container JavaDoc labelComponent = getLabelComponent();
138     if (labelComponent instanceof JLabel) {
139       return ((JLabel) labelComponent).getText();
140     } else {
141       return getProperty();
142     }
143   }
144
145   /**
146    * Accepts or rejects the initial focus for this component.
147    */

148   public boolean acceptDefaultFocus() {
149     if (isEditorEnabled()) {
150       // for some reason this returns false in dialogs
151
/*
152       boolean returnValue = valueComponent.requestFocusInWindow();
153       System.err.println( getDisplayValue() + ": returining " + returnValue);
154       return returnValue;
155       */

156       valueComponent.requestFocusInWindow();
157       return true;
158     } else {
159       return false;
160     }
161   }
162 }
163
Popular Tags