001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.ui.config;
038:
039: import javax.swing.*;
040: import javax.swing.border.EmptyBorder;
041: import java.awt.event.*;
042: import java.awt.*;
043:
044: // TODO: Check synchronization.
045: import java.util.Vector;
046:
047: /** The panel that set of configuration options (e.g. Fonts, Colors) uses to display its configurable items as read
048: * from OptionConstants.
049: * @version $Id: ConfigPanel.java 4255 2007-08-28 19:17:37Z mgricken $
050: */
051: public class ConfigPanel extends JPanel {
052:
053: protected final String _title;
054: protected final Vector<OptionComponent> _components;
055:
056: /** Constructor for this ConfigPanel
057: * @param title the title for this panel
058: */
059: public ConfigPanel(String title) {
060: //_title = new JLabel(title);
061: _title = title;
062: _components = new Vector<OptionComponent>();
063: }
064:
065: public String getTitle() {
066: return _title;
067: }
068:
069: /** The method for adding new OptionComponents to this ConfigPanel
070: * @param oc the OptionComponent to be added
071: */
072: public void addComponent(OptionComponent oc) {
073: _components.add(oc);
074: }
075:
076: public void displayComponents() {
077: this .setLayout(new BorderLayout());
078:
079: JPanel panel = new JPanel(); // sits in scrollpane and compresses layout
080: panel.setLayout(new BorderLayout());
081: JPanel panel2 = new JPanel(); // contains OptionComponents
082: panel.add(panel2, BorderLayout.NORTH);
083:
084: JScrollPane scroll = new JScrollPane(panel,
085: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
086: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
087: scroll.setBorder(BorderFactory.createTitledBorder(BorderFactory
088: .createEtchedBorder(), _title));
089:
090: // Fix increment on scrollbar
091: JScrollBar bar = scroll.getVerticalScrollBar();
092: bar.setUnitIncrement(25);
093: bar.setBlockIncrement(400);
094:
095: GridBagLayout gridbag = new GridBagLayout();
096: GridBagConstraints c = new GridBagConstraints();
097: panel2.setLayout(gridbag);
098: c.fill = GridBagConstraints.HORIZONTAL;
099: Insets labelInsets = new Insets(0, 10, 0, 10);
100: Insets compInsets = new Insets(0, 0, 0, 0);
101: for (int i = 0; i < _components.size(); i++) {
102: OptionComponent comp = _components.get(i);
103:
104: c.weightx = 0.0;
105: c.gridwidth = 1;
106: c.insets = labelInsets;
107:
108: JLabel label = comp.getLabel();
109: gridbag.setConstraints(label, c);
110: panel2.add(label);
111:
112: c.weightx = 1.0;
113: c.gridwidth = GridBagConstraints.REMAINDER;
114: c.insets = compInsets;
115:
116: JComponent otherC = comp.getComponent();
117: gridbag.setConstraints(otherC, c);
118: panel2.add(otherC);
119: }
120: /*
121: for (int i=0; i<_components.size(); i++) {
122: panel2.add(_components.get(i));
123: }*/
124:
125: // Reset Button
126: JButton _resetToDefaultButton = new JButton("Reset to Defaults");
127: _resetToDefaultButton.addActionListener(new ActionListener() {
128: public void actionPerformed(ActionEvent e) {
129: resetToDefault();
130: }
131: });
132: JPanel resetPanel = new JPanel();
133: resetPanel.setLayout(new FlowLayout());
134: resetPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
135: resetPanel.add(_resetToDefaultButton);
136: panel.add(resetPanel, BorderLayout.SOUTH);
137:
138: this .add(scroll, BorderLayout.CENTER);
139: }
140:
141: /** Tells each component in the vector to update Config with its value
142: * @return whether update() of all the components succeeded
143: */
144: public boolean update() {
145:
146: for (int i = 0; i < _components.size(); i++) {
147: boolean isValidUpdate = _components.get(i).updateConfig();
148: if (!isValidUpdate)
149: return false;
150: }
151: return true;
152: }
153:
154: /** Tells each component to reset its display field to the current value. */
155: public void resetToCurrent() {
156: for (int i = 0; i < _components.size(); i++)
157: _components.get(i).resetToCurrent();
158: }
159:
160: /** Tells each component to reset its value to the component's default. */
161: public void resetToDefault() {
162: for (int i = 0; i < _components.size(); i++)
163: _components.get(i).resetToDefault();
164: }
165: }
|