KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > editors > LineNumberPropertyEditor


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.core.editors;
21
22 import java.beans.PropertyEditorSupport JavaDoc;
23 import javax.swing.JLabel JavaDoc;
24 import javax.swing.SwingConstants JavaDoc;
25 import org.openide.ErrorManager;
26 import org.openide.text.Line;
27 import org.openide.util.NbBundle;
28
29 /**
30  * PropertyEditor for line numbers.
31  */

32 public final class LineNumberPropertyEditor extends PropertyEditorSupport JavaDoc {
33     private static final JLabel JavaDoc LABEL = new JLabel JavaDoc();
34
35     static {
36         LABEL.setHorizontalAlignment(SwingConstants.RIGHT);
37     }
38
39     public boolean isPaintable() {
40         return true;
41     }
42
43     public void paintValue(java.awt.Graphics JavaDoc gfx, java.awt.Rectangle JavaDoc box) {
44         if (box.width <= 3)
45             return;
46         
47         gfx.translate(box.x, box.y);
48         LABEL.setText(getAsText());
49         LABEL.setSize(box.width - 3, box.height);
50         LABEL.setForeground(gfx.getColor());
51         LABEL.paint(gfx);
52         gfx.translate(-box.x, -box.y);
53     }
54
55     public void setAsText(String JavaDoc text) throws java.lang.IllegalArgumentException JavaDoc {
56         try {
57             if (text.trim().length() == 0)
58                 setValue(new Integer JavaDoc(0));
59             else
60                 setValue(new Integer JavaDoc(Integer.parseInt(text)));
61         } catch (NumberFormatException JavaDoc nfe) {
62             String JavaDoc msg = NbBundle.getMessage(LineNumberPropertyEditor.class,
63                 "IllegalLineNumber", new Object JavaDoc[] {text}); //NOI18N
64
RuntimeException JavaDoc iae = new IllegalArgumentException JavaDoc(msg); //NOI18N
65
ErrorManager.getDefault().annotate(iae, ErrorManager.USER, msg,
66                 msg, nfe, new java.util.Date JavaDoc());
67             throw iae;
68         }
69     }
70     
71     public String JavaDoc getAsText() {
72         Object JavaDoc v = getValue();
73         String JavaDoc s;
74         if (v instanceof Line) {
75             s = String.valueOf(((Line) v).getLineNumber() + 1);
76         } else if (v instanceof Integer JavaDoc) {
77             int n = ((Integer JavaDoc) v).intValue();
78             if (n <= 0)
79                 s = "";
80             else
81                 s = String.valueOf(n);
82         } else {
83             s = "";
84         }
85         return s;
86     }
87 }
88
Popular Tags