OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [examples/] [gnu/] [classpath/] [examples/] [swing/] [TreeDemo.java] - Blame information for rev 781

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 781 jeremybenn
/* TreeDemo.java -- Demostrates JTree
2
   Copyright (C) 2006 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
 
39
package gnu.classpath.examples.swing;
40
 
41
import java.awt.BorderLayout;
42
import java.awt.event.ActionEvent;
43
import java.awt.event.ActionListener;
44
 
45
import javax.swing.ButtonGroup;
46
import javax.swing.JButton;
47
import javax.swing.JCheckBox;
48
import javax.swing.JComponent;
49
import javax.swing.JFrame;
50
import javax.swing.JLabel;
51
import javax.swing.JPanel;
52
import javax.swing.JRadioButton;
53
import javax.swing.JScrollPane;
54
import javax.swing.JTree;
55
import javax.swing.SwingUtilities;
56
import javax.swing.event.TreeSelectionEvent;
57
import javax.swing.event.TreeSelectionListener;
58
import javax.swing.tree.DefaultMutableTreeNode;
59
import javax.swing.tree.DefaultTreeModel;
60
import javax.swing.tree.DefaultTreeSelectionModel;
61
import javax.swing.tree.TreePath;
62
 
63
public class TreeDemo
64
  extends JPanel
65
  implements ActionListener
66
{
67
 
68
  TreeDemo()
69
  {
70
    super();
71
    createContent();
72
  }
73
 
74
  private void createContent()
75
  {
76
    // non-leafs
77
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Exotic Subsistence");
78
    DefaultMutableTreeNode fruit = new DefaultMutableTreeNode("Interesting Fruit");
79
    DefaultMutableTreeNode veg = new DefaultMutableTreeNode("Extraordinary Vegetables");
80
    DefaultMutableTreeNode liq = new DefaultMutableTreeNode("Peculiar Liquids");
81
 
82
    // leafs
83
    DefaultMutableTreeNode f1 = new DefaultMutableTreeNode("Abiu");
84
    DefaultMutableTreeNode f2 = new DefaultMutableTreeNode("Bamboo Shoots");
85
    DefaultMutableTreeNode f3 = new DefaultMutableTreeNode("Breadfruit");
86
    DefaultMutableTreeNode f4 = new DefaultMutableTreeNode("Canistel");
87
    DefaultMutableTreeNode f5 = new DefaultMutableTreeNode("Duku");
88
    DefaultMutableTreeNode f6 = new DefaultMutableTreeNode("Guava");
89
    DefaultMutableTreeNode f7 = new DefaultMutableTreeNode("Jakfruit");
90
    DefaultMutableTreeNode f8 = new DefaultMutableTreeNode("Quaribea");
91
 
92
    DefaultMutableTreeNode v1 = new DefaultMutableTreeNode("Amaranth");
93
    DefaultMutableTreeNode v2 = new DefaultMutableTreeNode("Kiwano");
94
    DefaultMutableTreeNode v3 = new DefaultMutableTreeNode("Leeks");
95
    DefaultMutableTreeNode v4 = new DefaultMutableTreeNode("Luffa");
96
    DefaultMutableTreeNode v5 = new DefaultMutableTreeNode("Chayote");
97
    DefaultMutableTreeNode v6 = new DefaultMutableTreeNode("Jicama");
98
    DefaultMutableTreeNode v7 = new DefaultMutableTreeNode("Okra");
99
 
100
    DefaultMutableTreeNode l1 = new DefaultMutableTreeNode("Alcoholic");
101
    DefaultMutableTreeNode l11 = new DefaultMutableTreeNode("Caipirinha");
102
    DefaultMutableTreeNode l21 = new DefaultMutableTreeNode("Mojito");
103
    DefaultMutableTreeNode l31 = new DefaultMutableTreeNode("Margarita");
104
    DefaultMutableTreeNode l41 = new DefaultMutableTreeNode("Martini");
105
    DefaultMutableTreeNode l5 = new DefaultMutableTreeNode("Non Alcoholic");
106
    DefaultMutableTreeNode l55 = new DefaultMutableTreeNode("Babaji");
107
    DefaultMutableTreeNode l65 = new DefaultMutableTreeNode("Chikita");
108
 
109
    root.add(fruit);
110
    root.add(veg);
111
    root.add(liq);
112
    fruit.add(f1);
113
    fruit.add(f2);
114
    fruit.add(f3);
115
    fruit.add(f4);
116
    fruit.add(f5);
117
    fruit.add(f6);
118
    fruit.add(f7);
119
    fruit.add(f8);
120
    veg.add(v1);
121
    veg.add(v2);
122
    veg.add(v3);
123
    veg.add(v4);
124
    veg.add(v5);
125
    veg.add(v6);
126
    veg.add(v7);
127
    liq.add(l1);
128
    l1.add(l11);
129
    l1.add(l21);
130
    l1.add(l31);
131
    l1.add(l41);
132
    liq.add(l5);
133
    l5.add(l55);
134
    l5.add(l65);
135
 
136
    final JTree tree = new JTree(root);
137
    tree.setLargeModel(true);
138
    tree.setEditable(true);
139
    final DefaultTreeSelectionModel selModel = new DefaultTreeSelectionModel();
140
    selModel.setSelectionMode(
141
      DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
142
    tree.setSelectionModel(selModel);
143
 
144
    // buttons to add and delete
145
    JButton add = new JButton("add element");
146
    add.addActionListener(new ActionListener()
147
      {
148
        public void actionPerformed(ActionEvent e)
149
        {
150
           for (int i = 0; i < tree.getRowCount(); i++)
151
           {
152
              if (tree.isRowSelected(i))
153
              {
154
                 TreePath p = tree.getPathForRow(i);
155
                 DefaultMutableTreeNode n = (DefaultMutableTreeNode) p.
156
                                                  getLastPathComponent();
157
                 n.add(new DefaultMutableTreeNode("New Element"));
158
 
159
                 // The expansion state of the parent node does not change
160
                 // by default. We will expand it manually, to ensure that the
161
                 // added node is immediately visible.
162
                 tree.expandPath(p);
163
 
164
                 // Refresh the tree (.repaint would be not enough both in
165
                 // Classpath and Sun implementations).
166
                 DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
167
                 model.reload(n);
168
                 break;
169
              }
170
           }
171
        }
172
      });
173
 
174
    // Demonstration of the various selection modes
175
    final JCheckBox cbSingle = new JCheckBox("single selection");
176
    cbSingle.addActionListener(new ActionListener()
177
      {
178
        public void actionPerformed(ActionEvent e)
179
      {
180
        if (cbSingle.isSelected())
181
          selModel.setSelectionMode(
182
            DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
183
        else
184
          selModel.setSelectionMode(
185
            DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
186
      }
187
      });
188
 
189
    // Demonstration of the root visibility changes
190
    final JCheckBox cbRoot = new JCheckBox("root");
191
    cbRoot.addActionListener(new ActionListener()
192
      {
193
        public void actionPerformed(ActionEvent e)
194
      {
195
        tree.setRootVisible(cbRoot.isSelected());
196
      }
197
      });
198
    cbRoot.setSelected(true);
199
 
200
    // Demonstration of the tree selection listener.
201
    final JLabel choice = new JLabel("Make a choice");
202
    tree.getSelectionModel().addTreeSelectionListener(
203
      new TreeSelectionListener()
204
        {
205
          public void valueChanged(TreeSelectionEvent event)
206
          {
207
            TreePath was = event.getOldLeadSelectionPath();
208
            TreePath now = event.getNewLeadSelectionPath();
209
            String swas =
210
              was == null ? "none":was.getLastPathComponent().toString();
211
            String snow =
212
              now == null ? "none":now.getLastPathComponent().toString();
213
            choice.setText("From "+swas+" to "+snow);
214
          }
215
        }
216
      );
217
 
218
    setLayout(new BorderLayout());
219
 
220
    JPanel p2 = new JPanel();
221
    p2.add(add);
222
    p2.add(cbSingle);
223
    p2.add(cbRoot);
224
 
225
    tree.getSelectionModel().
226
      setSelectionMode(DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
227
 
228
    // Panel for selecting line style.
229
    ActionListener l = new ActionListener()
230
    {
231
      public void actionPerformed(ActionEvent e)
232
      {
233
        JRadioButton b = (JRadioButton) e.getSource();
234
        tree.putClientProperty("JTree.lineStyle", b.getText());
235
        tree.repaint();
236
      }
237
    };
238
    JPanel lineStylePanel = new JPanel();
239
    ButtonGroup buttons = new ButtonGroup();
240
    lineStylePanel.add(new JLabel("Line style: "));
241
    JRadioButton none = new JRadioButton("None");
242
    lineStylePanel.add(none);
243
    buttons.add(none);
244
    none.addActionListener(l);
245
    JRadioButton angled = new JRadioButton("Angled");
246
    lineStylePanel.add(angled);
247
    buttons.add(angled);
248
    angled.addActionListener(l);
249
    JRadioButton horizontal = new JRadioButton("Horizontal");
250
    lineStylePanel.add(horizontal);
251
    buttons.add(horizontal);
252
    horizontal.addActionListener(l);
253
    p2.add(lineStylePanel);
254
 
255
    add(p2, BorderLayout.NORTH);
256
 
257
    add(new JScrollPane(tree), BorderLayout.CENTER);
258
    add(choice, BorderLayout.SOUTH);
259
  }
260
 
261
  public void actionPerformed(ActionEvent e)
262
  {
263
    if (e.getActionCommand().equals("CLOSE"))
264
    {
265
      System.exit(0);
266
    }
267
  }
268
 
269
  /**
270
   * When the demo is run independently, the frame is displayed, so we should
271
   * initialise the content panel (including the demo content and a close
272
   * button).  But when the demo is run as part of the Swing activity board,
273
   * only the demo content panel is used, the frame itself is never displayed,
274
   * so we can avoid this step.
275
   */
276
  void initFrameContent()
277
  {
278
    JPanel closePanel = new JPanel();
279
    JButton closeButton = new JButton("Close");
280
    closeButton.setActionCommand("CLOSE");
281
    closeButton.addActionListener(this);
282
    closePanel.add(closeButton);
283
    add(closePanel, BorderLayout.SOUTH);
284
  }
285
 
286
  public static void main(String[] args)
287
  {
288
    SwingUtilities.invokeLater
289
    (new Runnable()
290
     {
291
       public void run()
292
       {
293
         TreeDemo app = new TreeDemo();
294
         app.initFrameContent();
295
         JFrame frame = new JFrame("Tree Demo");
296
         frame.getContentPane().add(app);
297
         frame.pack();
298
         frame.setVisible(true);
299
       }
300
     });
301
  }
302
 
303
  /**
304
   * Returns a DemoFactory that creates a TreeDemo.
305
   *
306
   * @return a DemoFactory that creates a TreeDemo
307
   */
308
  public static DemoFactory createDemoFactory()
309
  {
310
    return new DemoFactory()
311
    {
312
      public JComponent createDemo()
313
      {
314
        return new TreeDemo();
315
      }
316
    };
317
  }
318
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.