View Javadoc

1   package org.lsst.ccs.plugin.jas3.tutorial;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Component;
5   import java.awt.FlowLayout;
6   import java.awt.Point;
7   import java.awt.event.ActionEvent;
8   import java.awt.event.ActionListener;
9   import javax.swing.JButton;
10  import javax.swing.JCheckBox;
11  import javax.swing.JEditorPane;
12  import javax.swing.JLabel;
13  import javax.swing.JMenuItem;
14  import javax.swing.JOptionPane;
15  import javax.swing.JPanel;
16  import javax.swing.JPopupMenu;
17  import javax.swing.JTextField;
18  import javax.swing.event.DocumentEvent;
19  import javax.swing.event.DocumentListener;
20  import org.freehep.application.mdi.ManagedPage;
21  import org.freehep.application.mdi.PageContext;
22  import org.freehep.swing.popup.HasPopupItems;
23  
24  /**
25   * This is a more advanced tutorial page. This page listens for life-cycle notifications, has its
26   * own popup menu, and various controls for closing itself.
27   * @author tonyj
28   */
29  public class AdvancedTutorialPage extends JPanel implements ManagedPage, HasPopupItems {
30      private PageContext pageContext;
31      private JCheckBox vetoCheckBox;
32      private JTextField titleField;
33  
34       public AdvancedTutorialPage() {
35          super(new BorderLayout());
36          JEditorPane text = new JEditorPane("text/html",
37                  "I am an AdvancedTutorialPage. Close me using my popup menu, or by clicking the "
38                  + "x in my page tab or the close button below."
39                  + "<p>I print messages to standard output when I am selected or deselected which should be "
40                  + "trapped and appear in a console window below."
41                  + "<p>I have a custom popup menu with an extra item added."
42                  + "<p>You can veto close operations, and change my title using the controls at the bottom "
43                  + "of the page.");
44          add(text,BorderLayout.CENTER);
45          add(createControls(),BorderLayout.SOUTH);
46      }
47  
48      @Override
49      public boolean close() {
50          // I am being asked to close, I can veto the request by returning false
51          if (vetoCheckBox.isSelected()) {
52              JOptionPane.showMessageDialog(this,"I refuse to close!");
53              return false;
54          }
55          return true;
56      }
57  
58      @Override
59      public void setPageContext(PageContext pc) {
60          this.pageContext = pc;
61          titleField.setText(pc.getTitle());
62          titleField.getDocument().addDocumentListener(new DocumentListener(){
63  
64              @Override
65              public void insertUpdate(DocumentEvent e) {
66                  updateTitle();
67              }
68  
69              @Override
70              public void removeUpdate(DocumentEvent e) {
71                  updateTitle();
72              }
73  
74              @Override
75              public void changedUpdate(DocumentEvent e) {
76                  updateTitle();
77              }
78  
79              private void updateTitle() {
80                  pageContext.setTitle(titleField.getText());
81              }
82          });
83  
84      }
85  
86      @Override
87      public void pageSelected() {
88          System.out.println("I've been selected");
89      }
90  
91      @Override
92      public void pageDeselected() {
93          System.out.println("I've been deselected");
94      }
95  
96      @Override
97      public void pageIconized() {
98          System.out.println("I've been iconized");
99      }
100 
101     @Override
102     public void pageDeiconized() {
103         System.out.println("I've been de-iconized");
104     }
105 
106     @Override
107     public void pageClosed() {
108         System.out.println("I've been closed");
109     }
110 
111     @Override
112     public JPopupMenu modifyPopupMenu(JPopupMenu menu, Component source, Point p) {
113         // Add a new item to the default popup menu.
114         JMenuItem extra = new JMenuItem("Extra");
115         extra.addActionListener(new ActionListener(){
116 
117             @Override
118             public void actionPerformed(ActionEvent e) {
119                 JOptionPane.showMessageDialog(AdvancedTutorialPage.this,"Extra menu item selected!");
120             }
121         });
122         // Add at top of existing menu
123         menu.insert(extra, 0);
124         return menu;
125     }
126 
127     // Create some demonstration fields at the bottom of the page. (In general I recommend
128     // using a GUI builder to create controls, but this is just a demo so I wanted to keep
129     // things simple.
130     private JPanel createControls() {
131         JPanel controls = new JPanel(new FlowLayout());
132         JButton closeButton = new JButton("Close");
133         closeButton.addActionListener(new ActionListener(){
134 
135             @Override
136             public void actionPerformed(ActionEvent e) {
137                 pageContext.close();
138             }
139         });
140         controls.add(closeButton);
141         vetoCheckBox = new JCheckBox("Veto close");
142         controls.add(vetoCheckBox);
143         controls.add(new JLabel("Title:"));
144         titleField = new JTextField(20);
145         controls.add(titleField);
146         return controls;
147     }
148     
149 }