KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > assistant > AssistantView


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 package org.netbeans.modules.form.assistant;
20
21 import java.awt.*;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import javax.swing.*;
28
29 import org.jdesktop.layout.GroupLayout;
30 import org.jdesktop.layout.LayoutStyle;
31
32 import org.netbeans.modules.form.FormLoaderSettings;
33 import org.openide.util.Utilities;
34
35 /**
36  * Assistant view.
37  *
38  * @author Jan Stola
39  */

40 public class AssistantView extends JPanel {
41     private JLabel messageLabel;
42     private AssistantModel model;
43     
44     public AssistantView(AssistantModel model) {
45         this.model = model;
46
47         Listener JavaDoc listener = new Listener JavaDoc();
48         model.addPropertyChangeListener(listener);
49
50         setBackground(FormLoaderSettings.getInstance().getFormDesignerBackgroundColor());
51         setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, getForeground()));
52         setToolTipText(null);
53
54         // Message label
55
messageLabel = new JLabel();
56         messageLabel.setIcon(new ImageIcon(Utilities.loadImage("org/netbeans/modules/form/resources/lightbulb.gif"))); // NOI18N
57

58         // Close button
59
JButton closeButton = new JButton("x"); // NOI18N
60
closeButton.setFont(Font.getFont("SansSerif")); // NOI18N
61
closeButton.setOpaque(false);
62         closeButton.setFocusPainted(false);
63         closeButton.setMargin(new Insets(0, 0, 0, 0));
64         closeButton.setBorderPainted(false);
65         closeButton.setContentAreaFilled(false);
66         closeButton.addActionListener(listener);
67         // Workaround for GroupLayout.BASELINE == GroupLayout.CENTER bug
68
JPanel panel = new JPanel(new BorderLayout(0,0));
69         panel.setOpaque(false);
70         panel.add(closeButton);
71
72         GroupLayout layout = new GroupLayout(this);
73         setLayout(layout);
74         layout.setHorizontalGroup(
75             layout.createSequentialGroup()
76                 .add(12)
77                 .add(messageLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
78                 .addPreferredGap(LayoutStyle.RELATED)
79                 .add(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
80                 .add(12));
81         layout.setVerticalGroup(
82             layout.createSequentialGroup()
83                 .add(2)
84                 .add(layout.createParallelGroup(GroupLayout.CENTER)
85                     .add(messageLabel, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
86                     .add(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
87                 .add(2));
88     }
89
90     private class Listener implements ActionListener JavaDoc, PropertyChangeListener JavaDoc {
91
92         public void actionPerformed(ActionEvent JavaDoc e) {
93             FormLoaderSettings.getInstance().setAssistantShown(false);
94         }
95
96         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
97             String JavaDoc[] messages = model.getMessages();
98             String JavaDoc message = null;
99             if (messages != null) {
100                 int index = (int)(Math.random()*messages.length);
101                 message = messages[index];
102             }
103             if (model.getAdditionalContext() != null) {
104                 messages = model.getAdditionalMessages();
105                 if (messages != null) {
106                     int index = (int)(Math.random()*messages.length);
107                     message = "<html>" + message + "<br>" + messages[index]; // NOI18N
108
}
109             }
110             Object JavaDoc[] params = model.getParameters();
111             if (params != null) {
112                 message = MessageFormat.format(message, params);
113             }
114             messageLabel.setText(message);
115         }
116
117     }
118
119 }
120
Popular Tags