KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > config > ConfigPanel


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui.config;
35
36 import javax.swing.*;
37 import javax.swing.border.EmptyBorder JavaDoc;
38 import java.awt.event.*;
39 import java.awt.*;
40
41 // TODO: Check synchronization.
42
import java.util.Vector JavaDoc;
43
44 /** The panel that set of configuration options (e.g. Fonts, Colors) uses to display its configurable items as read
45  * from OptionConstants.
46  * @version $Id: ConfigPanel.java 3877 2006-06-08 22:29:32Z rcartwright $
47  */

48 public class ConfigPanel extends JPanel {
49
50   protected final String JavaDoc _title;
51   protected final Vector JavaDoc<OptionComponent> _components;
52
53   /** Constructor for this ConfigPanel
54    * @param title the title for this panel
55    */

56   public ConfigPanel(String JavaDoc title) {
57     //_title = new JLabel(title);
58
_title = title;
59     _components = new Vector JavaDoc<OptionComponent>();
60   }
61
62   public String JavaDoc getTitle() { return _title; }
63
64   /** The method for adding new OptionComponents to this ConfigPanel
65    * @param oc the OptionComponent to be added
66    */

67   public void addComponent( OptionComponent oc) { _components.add(oc); }
68
69   public void displayComponents() {
70     this.setLayout(new BorderLayout());
71
72     JPanel panel = new JPanel(); // sits in scrollpane and compresses layout
73
panel.setLayout(new BorderLayout());
74     JPanel panel2 = new JPanel(); // contains OptionComponents
75
panel.add(panel2, BorderLayout.NORTH);
76     
77     JScrollPane scroll =
78       new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
79     scroll.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), _title));
80     
81     // Fix increment on scrollbar
82
JScrollBar bar = scroll.getVerticalScrollBar();
83     bar.setUnitIncrement(25);
84     bar.setBlockIncrement(400);
85
86     GridBagLayout gridbag = new GridBagLayout();
87     GridBagConstraints c = new GridBagConstraints();
88     panel2.setLayout(gridbag);
89     c.fill = GridBagConstraints.HORIZONTAL;
90     Insets labelInsets = new Insets(0, 10, 0, 10);
91     Insets compInsets = new Insets(0, 0, 0, 0);
92     for (int i=0; i<_components.size(); i++) {
93       OptionComponent comp = _components.get(i);
94
95       c.weightx = 0.0;
96       c.gridwidth = 1;
97       c.insets = labelInsets;
98
99       JLabel label= comp.getLabel();
100       gridbag.setConstraints(label, c);
101       panel2.add(label);
102
103       c.weightx = 1.0;
104       c.gridwidth = GridBagConstraints.REMAINDER;
105       c.insets = compInsets;
106
107       JComponent otherC = comp.getComponent();
108       gridbag.setConstraints(otherC, c);
109       panel2.add(otherC);
110     }
111     /*
112      for (int i=0; i<_components.size(); i++) {
113      panel2.add(_components.get(i));
114      }*/

115
116     // Reset Button
117
JButton _resetToDefaultButton = new JButton("Reset to Defaults");
118     _resetToDefaultButton.addActionListener(new ActionListener() {
119       public void actionPerformed(ActionEvent e) { resetToDefault(); }
120     });
121     JPanel resetPanel = new JPanel();
122     resetPanel.setLayout(new FlowLayout());
123     resetPanel.setBorder(new EmptyBorder JavaDoc(5,5,5,5));
124     resetPanel.add(_resetToDefaultButton);
125     panel.add(resetPanel, BorderLayout.SOUTH);
126
127     this.add(scroll, BorderLayout.CENTER);
128   }
129
130   /** Tells each component in the vector to update Config with its value
131    * @return whether update() of all the components succeeded
132    */

133   public boolean update() {
134
135     for (int i = 0; i < _components.size();i++) {
136       boolean isValidUpdate = _components.get(i).updateConfig();
137       if (! isValidUpdate) return false;
138     }
139     return true;
140   }
141
142   /** Tells each component to reset its display field to the current value. */
143   public void resetToCurrent() {
144     for (int i=0; i < _components.size(); i++) _components.get(i).resetToCurrent();
145   }
146
147   /** Tells each component to reset its value to the component's default. */
148   public void resetToDefault() {
149     for (int i=0; i < _components.size(); i++) _components.get(i).resetToDefault();
150   }
151 }
152
Popular Tags