root/trunk/plugins/examples/org/pathvisio/example/ExPreferences.java

Revision 2979, 3.1 KB (checked in by martijn, 8 months ago)

For the sake of uniformity, removed all trailing whitespace in Java code

Line 
1// PathVisio,
2// a tool for data visualization and analysis using Biological Pathways
3// Copyright 2006-2009 BiGCaT Bioinformatics
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17package org.pathvisio.example;
18
19import java.awt.event.ActionEvent;
20
21import javax.swing.AbstractAction;
22import javax.swing.JFileChooser;
23
24import org.pathvisio.gui.swing.PreferencesDlg;
25import org.pathvisio.gui.swing.PvDesktop;
26import org.pathvisio.plugin.Plugin;
27import org.pathvisio.preferences.Preference;
28import org.pathvisio.preferences.PreferenceManager;
29
30/**
31 * Example of how to use Preference Manager and Preferences Dlg
32 */
33public class ExPreferences implements Plugin {
34
35        private PvDesktop desktop;
36
37        /**
38         * Defines three example preferences
39         */
40        enum ExPreference implements Preference
41        {
42                /**
43                 * This will be the last opened dir of a JFileChooser.
44                 * This preference will be stored but not available
45                 */
46                EXAMPLE_LAST_OPENED_DIR (System.getProperty("user.home")),
47
48                /**
49                 * An integer preference, available through the Example tab
50                 * of the preference manager
51                 */
52                EXAMPLE_INT ("" + 42),
53
54                /**
55                 * A Color preference, available through the Example tab
56                 * of the preference manager
57                 */
58                EXAMPLE_COLOR ("255,0,0");
59
60                ExPreference (String defaultValue)
61                {
62                        this.defaultValue = defaultValue;
63                }
64
65                private String defaultValue;
66
67                public String getDefault() {
68                        return defaultValue;
69                }
70        }
71
72        public void init(PvDesktop desktop)
73        {
74                this.desktop = desktop;
75
76                PreferencesDlg dlg = desktop.getPreferencesDlg();
77
78                dlg.addPanel("Example Plugin",
79                                dlg.builder()
80                                        .colorField(ExPreference.EXAMPLE_COLOR, "Example Color")
81                                        .integerField(ExPreference.EXAMPLE_INT, "Example Integer between 0 and 100", 0, 100)
82                                        .build()
83                                );
84
85                desktop.registerMenuAction("File", new ChooseFileAction());
86        }
87
88        private class ChooseFileAction extends AbstractAction
89        {
90                public ChooseFileAction()
91                {
92                        // This will be the label of the pop up menu item.
93                        putValue (NAME, "Example File Chooser");
94                }
95
96                public void actionPerformed(ActionEvent arg0)
97                {
98                        // Display a message with the actual class
99                        JFileChooser jfc = new JFileChooser();
100                        // set current directory to what was stored in the preferences
101                        // default: home directory.
102                        jfc.setCurrentDirectory(
103                                        PreferenceManager.getCurrent().getFile(ExPreference.EXAMPLE_LAST_OPENED_DIR));
104                        int status = jfc.showOpenDialog(desktop.getFrame());
105                        if(status == JFileChooser.APPROVE_OPTION)
106                        {
107                                // save current directory of jfc.
108                                // next time will be opened in same location.
109                                PreferenceManager.getCurrent().setFile(ExPreference.EXAMPLE_LAST_OPENED_DIR,
110                                                jfc.getCurrentDirectory());
111                        }
112                }
113        }
114
115        public void done() {}
116}
Note: See TracBrowser for help on using the browser.