KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > InplaceEditableLabel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * InplaceEditableLabel.java
22  *
23  * Created on June 9, 2006, 3:12 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.schema.abe;
30
31 import java.awt.Color JavaDoc;
32 import java.awt.Component JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.Font JavaDoc;
35 import java.awt.FontMetrics JavaDoc;
36 import java.awt.Graphics JavaDoc;
37 import java.awt.Rectangle JavaDoc;
38 import java.awt.event.FocusEvent JavaDoc;
39 import java.awt.event.FocusListener JavaDoc;
40 import java.awt.event.HierarchyEvent JavaDoc;
41 import java.awt.event.HierarchyListener JavaDoc;
42 import java.awt.event.KeyAdapter JavaDoc;
43 import java.awt.event.KeyEvent JavaDoc;
44 import java.awt.event.MouseAdapter JavaDoc;
45 import java.awt.event.MouseEvent JavaDoc;
46 import javax.swing.JComponent JavaDoc;
47 import javax.swing.JLabel JavaDoc;
48 import javax.swing.JPanel JavaDoc;
49 import javax.swing.JScrollPane JavaDoc;
50 import javax.swing.JTextField JavaDoc;
51 import javax.swing.SwingConstants JavaDoc;
52 import javax.swing.SwingUtilities JavaDoc;
53 import org.openide.util.NbBundle;
54
55 /**
56  *
57  * @author girix
58  */

59 public class InplaceEditableLabel extends JLabel JavaDoc{
60     private static final long serialVersionUID = 7526472295622776147L;
61     boolean editMode = false;
62     public static final String JavaDoc PROPERTY_MODE_CHANGE = "PROPERTY_MODE_CHANGE";
63     public enum Mode{
64         EDIT, NORMAL
65     };
66     boolean mouseOverMe = false;
67     boolean altKeyPressed = false;
68     private Color JavaDoc mouseOverColor;
69     private JComponent JavaDoc editorComponent;
70     JPanel JavaDoc glass;
71     //NBGlassPaneAccessSupport editorSupport;
72
/** Creates a new instance of InplaceEditableLabel */
73     public InplaceEditableLabel() {
74         super();
75         setForeground(Color.BLUE);
76         setMouseOverColor(getForeground());
77         setToolTipText(NbBundle.getMessage(InplaceEditableLabel.class,
78                 "TTP_DEFAULT_INPLACE_EDIT"));
79         initialize();
80     }
81     
82     public InplaceEditableLabel(String JavaDoc str){
83         this();
84         super.setText(str);
85     }
86     
87     public JScrollPane JavaDoc getScrollPane() {
88         return ((ABEBaseDropPanel) this.getParent()).context.getInstanceDesignerScrollPane();
89     }
90     
91     void initialize(){
92         addMouseListener(new MouseAdapter JavaDoc(){
93             public void mouseClicked(MouseEvent JavaDoc e) {
94                 //add logic to handle edits
95
if( ( (e.isShiftDown() && (e.getClickCount() == 1) ) ||
96                         (e.getClickCount() > 1)) ){
97                     //setEditMode
98
showEditor();
99                 }else if(drewLine){
100                     //call the ctrl click handler
101
if(ctrlClickHandler != null)
102                         ctrlClickHandler.handleCtrlClick();
103                 }else{
104                     InplaceEditableLabel.this.getParent().dispatchEvent(e);
105                 }
106             }
107             
108             public void mouseEntered(MouseEvent JavaDoc e) {
109                 super.mouseEntered(e);
110                 /*mouseOverMe = true;
111                 if(e.isControlDown()){
112                     altKeyPressed = true;
113                     //requestFocusInWindow();
114                 }*/

115                 repaint();
116                 InplaceEditableLabel.this.getParent().dispatchEvent(e);
117             }
118             
119             public void mouseExited(MouseEvent JavaDoc e) {
120                 super.mouseExited(e);
121                 /*mouseOverMe = false;
122                 altKeyPressed = false;
123                 repaint();*/

124                 InplaceEditableLabel.this.getParent().dispatchEvent(e);
125             }
126             
127             public void mouseReleased(MouseEvent JavaDoc e) {
128                 if(!drewLine)
129                     InplaceEditableLabel.this.getParent().dispatchEvent(e);
130             }
131             
132             public void mousePressed(MouseEvent JavaDoc e) {
133                 if(!drewLine)
134                     InplaceEditableLabel.this.getParent().dispatchEvent(e);
135             }
136             
137         });
138     }
139     
140     boolean drewLine = false;
141     protected void paintComponent(Graphics JavaDoc g) {
142         drewLine = false;
143         if(mouseOverMe && altKeyPressed && (ctrlClickHandler != null) ){
144             //draw diff color and underline
145
Color JavaDoc origC = getForeground();
146             Font JavaDoc origF = getFont();
147             setForeground(getMouseOverColor());
148             //g.setFont(new Font(origF.getName(), Font.BOLD, origF.getSize()));
149
Rectangle JavaDoc bounds = g.getClipBounds();
150             super.paintComponent(g);
151             g.setColor(getMouseOverColor());
152             int width = SwingUtilities.computeStringWidth(getFontMetrics(getFont()), getText());
153             g.drawLine(bounds.x, bounds.y + bounds.height -1,
154                     bounds.x + width, bounds.y + bounds.height -1);
155             setForeground(origC);
156             setFont(origF);
157             drewLine = true;
158         }else{
159             super.paintComponent(g);
160         }
161     }
162     
163     public Color JavaDoc getMouseOverColor() {
164         return mouseOverColor;
165     }
166     
167     public void setMouseOverColor(Color JavaDoc mouseOverColor) {
168         this.mouseOverColor = mouseOverColor;
169     }
170     
171     public void setText(String JavaDoc str){
172         super.setText(str);
173         Font JavaDoc font = getFont();
174         if(font != null){
175             FontMetrics JavaDoc fm = getFontMetrics(font);
176             int width = SwingUtilities.computeStringWidth(fm, str);
177             setPreferredSize(new Dimension JavaDoc(width, getPreferredSize().height));
178         }
179         if(editMode){
180             //this case will happen if the user selected something in edit mode
181
hideEditor();
182         }
183     }
184     
185     public void showEditor(){
186         firePropertyChange(PROPERTY_MODE_CHANGE, Mode.NORMAL, Mode.EDIT);
187         if((editorComponent == null) && (editInfoString == null))
188             return;
189         
190         editMode = true;
191         
192         //editorSupport = new NBGlassPaneAccessSupport();
193
glass = NBGlassPaneAccessSupport.getNBGlassPane(this);
194         if(glass == null)
195             return;
196         if(editorComponent != null){
197             Rectangle JavaDoc rect = SwingUtilities.convertRectangle(this.getParent(),
198                     this.getBounds(), glass);
199             glass.add(editorComponent);
200             rect.width = this.getMinimumSize().width * wMagFactor + 5;
201             rect.height += 7;
202             editorComponent.setBounds(rect);
203         }
204         addInfoLabel();
205         glass.setVisible(true);
206         
207         
208         if(editorComponent != null){
209             editorComponent.addKeyListener(new KeyAdapter JavaDoc() {
210                 public void keyTyped(KeyEvent JavaDoc e) {
211                     super.keyTyped(e);
212                     if(e.getKeyChar() == e.VK_ESCAPE)
213                         hideEditor();
214                     if(e.getKeyChar() == e.VK_ENTER){
215                         if(validInput())
216                             hideEditor();
217                     }
218                 }
219             });
220             editorComponent.addFocusListener(new FocusListener JavaDoc(){
221                 public void focusLost(FocusEvent JavaDoc e) {
222                     if(getParent() instanceof ABEBaseDropPanel){
223                         //if focus is lost while the DnD is not complete, ignore hide
224
ABEBaseDropPanel acp = (ABEBaseDropPanel) getParent();
225                         if(acp.context.isUserInducedEventMode())
226                             return;
227                     }
228                     hideEditor();
229                 }
230                 public void focusGained(FocusEvent JavaDoc e) {
231                 }
232             });
233             
234             this.addHierarchyListener(new HierarchyListener JavaDoc() {
235                 public void hierarchyChanged(HierarchyEvent JavaDoc e) {
236                     //remove when the inplace label is taken off
237
try{
238                         hideEditor();
239                     }catch (Exception JavaDoc ex){
240                         //ignore this
241
}
242                 }
243             });
244             //dont let the user scroll when the edit mode is on
245
getScrollPane().setWheelScrollingEnabled(false);
246             editorComponent.requestFocusInWindow();
247         }
248     }
249     
250     
251     public void hideEditor(){
252         editMode = false;
253         //restore scroll mode.
254
getScrollPane().setWheelScrollingEnabled(true);
255         NBGlassPaneAccessSupport.disposeNBGlassPane();
256         //refresh all selected components again for focus change.
257
((ABEBaseDropPanel) this.getParent()).context.
258                 getComponentSelectionManager().refreshFocus();
259     }
260     
261     public void setInlineEditorComponent(JComponent JavaDoc editorComponent){
262         this.editorComponent = editorComponent;
263     }
264     
265     
266     public Dimension JavaDoc getMaximumSize(){
267         return super.getMinimumSize();
268     }
269     
270     public Dimension JavaDoc getMinimumSize(){
271         return super.getPreferredSize();
272     }
273     
274     public Dimension JavaDoc getPreferredSize(){
275         return super.getMinimumSize();
276     }
277     
278     String JavaDoc editInfoString = null;
279     InstanceUIContext context = null;
280     public void setEditInfoText(String JavaDoc editInfoString, InstanceUIContext context){
281         this.editInfoString = editInfoString;
282         this.context = context;
283     }
284     
285     private void addInfoLabel() {
286         if(editInfoString == null)
287             return;
288         JLabel JavaDoc infoLabel = new JLabel JavaDoc(UIUtilities.getImageIcon("bulb.png"), SwingConstants.LEFT);
289         infoLabel.setText(editInfoString);
290         Component JavaDoc panel = this.getParent();
291         Rectangle JavaDoc rect = panel.getBounds();
292         rect = SwingUtilities.convertRectangle(panel.getParent(), rect, glass);
293         rect.y -= 20;
294         Dimension JavaDoc dim = infoLabel.getPreferredSize();
295         rect.width = dim.width;
296         rect.height = dim.height;
297         glass.add(infoLabel);
298         infoLabel.setBackground(new Color JavaDoc(255,255,220));
299         infoLabel.setOpaque(true);
300         infoLabel.setBounds(rect);
301     }
302     
303     int wMagFactor = 2;
304     public void setWidthMagnificationFactor(int factor){
305         this.wMagFactor = factor;
306     }
307     
308     InputValidator inputValidator;
309     String JavaDoc errorMessage;
310     public void setInputValidator(InputValidator iv, String JavaDoc errorMessage){
311         this.inputValidator = iv;
312         this.errorMessage = errorMessage;
313     }
314     
315     private boolean validInput(){
316         if(editorComponent instanceof JTextField JavaDoc){
317             String JavaDoc str = ((JTextField JavaDoc)editorComponent).getText();
318             if( (this.inputValidator != null) && (str != null) ){
319                 if(!this.inputValidator.isStringValid(str)){
320                     UIUtilities.showErrorMessageFor(errorMessage,
321                             ((ABEBaseDropPanel) this.getParent()).context, glass, editorComponent);
322                     return false;
323                 }else{
324                     UIUtilities.hideGlassMessage(false);
325                 }
326             }
327         }
328         return true;
329     }
330     
331     CtrlClickHandler ctrlClickHandler;
332     public void addCtrlClickHandler(CtrlClickHandler ctrlClickHandler){
333         this.ctrlClickHandler = ctrlClickHandler;
334     }
335     
336     public interface CtrlClickHandler{
337         public void handleCtrlClick();
338     }
339 }
340
Popular Tags