1 19 package org.netbeans.modules.form.assistant; 20 21 import java.awt.*; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.text.MessageFormat ; 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 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 listener = new Listener (); 48 model.addPropertyChangeListener(listener); 49 50 setBackground(FormLoaderSettings.getInstance().getFormDesignerBackgroundColor()); 51 setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, getForeground())); 52 setToolTipText(null); 53 54 messageLabel = new JLabel(); 56 messageLabel.setIcon(new ImageIcon(Utilities.loadImage("org/netbeans/modules/form/resources/lightbulb.gif"))); 58 JButton closeButton = new JButton("x"); closeButton.setFont(Font.getFont("SansSerif")); 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 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 , PropertyChangeListener { 91 92 public void actionPerformed(ActionEvent e) { 93 FormLoaderSettings.getInstance().setAssistantShown(false); 94 } 95 96 public void propertyChange(PropertyChangeEvent evt) { 97 String [] messages = model.getMessages(); 98 String 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]; } 109 } 110 Object [] 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 |