summaryrefslogtreecommitdiff
path: root/src/com/sun/apoc/tools/profileeditor/gui/PreferencesJPanel.java
blob: 532ece1c4d6367f6f50aa917482b3b53a923da31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
 * 
 * The contents of this file are subject to the terms of either
 * the GNU General Public License Version 2 only ("GPL") or
 * the Common Development and Distribution License("CDDL")
 * (collectively, the "License"). You may not use this file
 * except in compliance with the License. You can obtain a copy
 * of the License at www.sun.com/CDDL or at COPYRIGHT. See the
 * License for the specific language governing permissions and
 * limitations under the License. When distributing the software,
 * include this License Header Notice in each file and include
 * the License file at /legal/license.txt. If applicable, add the
 * following below the License Header, with the fields enclosed
 * by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Contributor(s):
 * 
 * If you wish your version of this file to be governed by
 * only the CDDL or only the GPL Version 2, indicate your
 * decision by adding "[Contributor] elects to include this
 * software in this distribution under the [CDDL or GPL
 * Version 2] license." If you don't indicate a single choice
 * of license, a recipient has the option to distribute your
 * version of this file under either the CDDL, the GPL Version
 * 2 or to extend the choice of license to its licensees as
 * provided above. However, if you add GPL Version 2 code and
 * therefore, elected the GPL Version 2 license, then the
 * option applies only if the new code is made subject to such
 * option by the copyright holder.
 */

package com.sun.apoc.tools.profileeditor.gui;

import com.sun.apoc.tools.profileeditor.ProfileEditorPreferences;
import com.sun.apoc.tools.profileeditor.gui.GradientJButton;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.Border;

/**
 *
 * @author cs202741
 */
public class PreferencesJPanel extends JPanel implements ActionListener{
    
    private ProfileEditorPreferences mPreferences = null;
    private JPanel mainPanel  = null;
    private ArrayList<Component> components = null;
    private JTabbedPane mTabbedPane = null;
    
    /** Creates a new instance of PreferencesPanel */
    public PreferencesJPanel(ProfileEditorPreferences aPreferences, JTabbedPane tabbedPane) {
        mPreferences = aPreferences;
        mTabbedPane = tabbedPane;
        components = new ArrayList();

        this.setBackground( Color.WHITE );
        this.setLayout( new BorderLayout() );

        mainPanel = new JPanel();
        mainPanel.setLayout( new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS) );

        mainPanel.add( getGeneral() );
        mainPanel.add( getButtons() );
        
        this.add( mainPanel, BorderLayout.PAGE_START);
    }
    
    private JPanel getGeneral(){

        TitledSectionJPanel titledPanel = new TitledSectionJPanel(new JLabel("General Preferences"));
        
        JPanel panel = new JPanel(new GridBagLayout());
        titledPanel.setContentPanel( panel, true );

        // displayResourceIds
        String value = mPreferences.getProperty( "displayResourceIds");
        addWithGridBag( new JLabel("<html>Display Resource ID's instead of default name<br>" +
                        "when no locale information is available?</html>"), panel, 
                        0, 0, 1, 1,
                        GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
                        0, 0);
        
        JCheckBox box = new JCheckBox();
        box.setOpaque(false);
        box.setSelected( Boolean.valueOf( value ) );
        box.setName( "displayResourceIds");
        components.add( box );
        addWithGridBag( box, panel, 
                        1, 0, 1, 1,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
                        0, 0);        
        
        
        // templatePackagePath
        value = mPreferences.getProperty( "templatePackagePath");
        addWithGridBag( new JLabel("Path to Template Package Root: "), panel, 
                        0, 1, 1, 1,
                        GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
                        0, 0);
        
        JTextField text = new JTextField( value );
        text.setName("templatePackagePath");
        components.add(text);
        addWithGridBag( text, panel, 
                        1, 1, 1, 1,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
                        0, 0);
        
        // userLocales
        value = mPreferences.getProperty( "userLocales");
        addWithGridBag( new JLabel("User Locales: "), panel, 
                        0, 2, 1, 1,
                        GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
                        0, 0);
        text = new JTextField( value );
        text.setName("userLocales");
        components.add(text);
        addWithGridBag( text, panel, 
                        1, 2, 1, 1,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
                        0, 0);

        addWithGridBag( new JLabel("Defaults: " + mPreferences.getProperty("defaultLocales")), panel, 
                        1, 3, 1, 1,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
                        0, 0);
        
        return titledPanel;
    }
    
    
    private JPanel getButtons(){
        
        Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);
        JPanel panel = new JPanel( new FlowLayout( FlowLayout.RIGHT, 12, 0));
        panel.setBackground( Color.WHITE );
        panel.setBorder( border );

        JButton ok = new JButton("Ok");
        ok.setActionCommand("Ok");
        ok.addActionListener(this);
        
        JButton reset = new JButton("Reset Defaults");
        reset.setActionCommand("ResetDefaults");
        reset.addActionListener(this);
        
        JButton cancel = new JButton("Cancel");
        cancel.setActionCommand("Cancel");
        cancel.addActionListener(this);
        
        panel.add(ok);
        panel.add(reset);
        panel.add(cancel);
        
        return panel;
    }
    
    
    private void addWithGridBag(Component comp, Container cont,
                                int x, int y,
                                int width, int height,
                                int anchor, int fill,
                                int weightx, int weighty){
        
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets( 10, 5, 10, 5);
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbc.anchor = anchor;
        gbc.fill = fill;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        cont.add(comp, gbc);
    }
    
    
    private void savePreferences(){
        Iterator it = components.iterator();
        
        while(it.hasNext()){
            Object comp = it.next();
            
            if( comp instanceof JTextField ){
                JTextField text = (JTextField)comp;
                if( !(text.getText().equals("")) ){
                    mPreferences.setProperty( text.getName(), text.getText() );
                }else
                    mPreferences.remove( text.getName() );   
            }else if( comp instanceof JCheckBox ){
                JCheckBox box = (JCheckBox)comp;
                mPreferences.setProperty( box.getName(), Boolean.toString( box.isSelected() ) );
            }
        }
        try {
            mPreferences.store();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    private void resetDefaults(){
        
    }

    public void actionPerformed(ActionEvent event) {
        
        if( event.getActionCommand().equals("Ok") ){
            savePreferences();
            mTabbedPane.remove( mTabbedPane.getSelectedIndex() );
            mTabbedPane.setSelectedIndex(0);
        }else if( event.getActionCommand().equals("Cancel") ){
            mTabbedPane.remove( mTabbedPane.getSelectedIndex() );
            mTabbedPane.setSelectedIndex(0);
        }else if( event.getActionCommand().equals("ResetDefaults") ){
            resetDefaults();
        }
    }
}