PageRenderTime 20ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/SideKick/sidekick/ModeOptionsDialog.java

#
Java | 206 lines | 133 code | 27 blank | 46 comment | 8 complexity | e441ebb1fdab38616fb79882a2388ba9 MD5 | raw file
  1/*
  2 * ModeOptionsDialog.java 
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2006 Alan Ezust
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22package sidekick;
 23
 24// {{{ imports
 25import java.awt.BorderLayout;
 26import java.awt.GridLayout;
 27import java.awt.event.ActionEvent;
 28import java.awt.event.ActionListener;
 29import java.util.Collections;
 30import java.util.Enumeration;
 31import java.util.Vector;
 32
 33import javax.swing.JCheckBox;
 34import javax.swing.JComboBox;
 35import javax.swing.JLabel;
 36import javax.swing.JPanel;
 37import javax.swing.event.TreeSelectionEvent;
 38
 39import org.gjt.sp.jedit.MiscUtilities;
 40import org.gjt.sp.jedit.OptionGroup;
 41import org.gjt.sp.jedit.ServiceManager;
 42import org.gjt.sp.jedit.View;
 43import org.gjt.sp.jedit.jEdit;
 44import org.gjt.sp.jedit.gui.OptionsDialog;
 45import org.gjt.sp.util.StandardUtilities;
 46import org.gjt.sp.util.StringList;
 47// }}}
 48
 49/** {{{ ModeOptionsDialog class 
 50 * A customized OptionDialog for
 51 * SideKick, which includes a shared ComboBox for the current edit
 52 * mode.
 53 * 
 54 * It creates an OptionPane for each plugin that defines the proper
 55 * service, and is currently loaded.
 56 * 
 57 * @see AbstractModeOptionPane 
 58 * @author Alan Ezust
 59 *
 60 */
 61
 62public class ModeOptionsDialog extends OptionsDialog
 63{
 64	// {{{ data members
 65	public static final String SERVICECLASS="org.gjt.sp.jedit.options.ModeOptionPane";
 66	public static final String DEFAULT=jEdit.getProperty("options.editing.global"); 
 67	public static final String ALL="ALL";
 68
 69	Vector<ModeOptionPane> panes;
 70	OptionTreeModel paneTreeModel;
 71	StringList modes;
 72	JComboBox modeCombo;	
 73	JCheckBox useDefaultsCheck;
 74	// }}}
 75	
 76	// {{{ ModeOptionsDialog ctor
 77	public ModeOptionsDialog(View v) {
 78		super(v, "options.mode.settings", "sidekick.mode");
 79	} // }}}
 80	
 81	// {{{ getMode()
 82	public String getMode() {
 83		return modeCombo.getSelectedItem().toString();
 84	} // }}}
 85	
 86	// {{{ createOptionTreeModel method
 87	protected OptionTreeModel createOptionTreeModel()
 88	{
 89		modes = new StringList(jEdit.getModes());
 90		
 91		Collections.sort(modes,new StandardUtilities.StringCompare(true));
 92		modes.add(0, DEFAULT);
 93		modeCombo = new JComboBox(modes.toArray());
 94		useDefaultsCheck = new JCheckBox(jEdit.getProperty("options.editing.useDefaults"));
 95		JLabel editModeLabel = new JLabel(jEdit.getProperty("buffer-options.mode"));
 96		
 97		GridLayout gl = new GridLayout(1, 3);
 98		JPanel editModePanel = new JPanel(gl);
 99		// JLabel spacer = new JLabel(" ");
100		//for (int i=0; i<3; ++i) editModePanel.add(spacer);
101
102		editModePanel.add(useDefaultsCheck);
103		editModePanel.add(editModeLabel);
104		editModePanel.add(modeCombo);
105		
106		JPanel content = (JPanel) getContentPane();
107		content.add(editModePanel, BorderLayout.NORTH);
108				
109		paneTreeModel = new OptionTreeModel();
110		OptionGroup root = (OptionGroup) (paneTreeModel.getRoot());
111		
112		panes = new Vector<ModeOptionPane>();
113		// iterate through all parsers and get their name, attempt to get an option pane.
114		for (String service: ServiceManager.getServiceNames(SERVICECLASS)) 
115		{
116			AbstractModeOptionPane mop = (AbstractModeOptionPane) ServiceManager.getService(SERVICECLASS, service);
117			root.addOptionPane(mop);
118			panes.add(mop);
119		}
120
121		modeCombo.addActionListener(new ActionListener() {
122			public void actionPerformed(ActionEvent event) {
123				modeSelected();
124			}
125		});
126		useDefaultsCheck.addActionListener(new ActionListener() {
127			public void actionPerformed(ActionEvent e) {
128				useDefaultsChanged();
129			}
130		});
131
132		String currentMode = jEdit.getActiveView().getBuffer().getMode().getName();
133		modeCombo.setSelectedItem(currentMode);
134
135		return paneTreeModel;
136	} // }}}
137
138	private void useDefaultsChanged() {
139		if (currentPane instanceof AbstractModeOptionPane)
140			((AbstractModeOptionPane)currentPane).setUseDefaults(
141				useDefaultsCheck.isSelected());
142	}
143
144	private void modeSelected()
145	{
146		int index = modeCombo.getSelectedIndex();
147		String mode;
148		if (index == 0) {
149			mode = null;
150			useDefaultsCheck.setEnabled(false);
151		} else {
152			mode = (String) modeCombo.getItemAt(index); 
153			useDefaultsCheck.setEnabled(true);
154		}
155		if (currentPane instanceof AbstractModeOptionPane)
156		{
157			AbstractModeOptionPane current = (AbstractModeOptionPane)currentPane; 
158			current.modeSelected(mode);
159			useDefaultsCheck.setSelected(current.getUseDefaults(mode));
160		}
161		else
162		{
163			useDefaultsCheck.setSelected(false);
164		}
165	}
166	
167	@Override
168	public void valueChanged(TreeSelectionEvent evt) {
169		super.valueChanged(evt);
170		modeSelected();	// Update the current pane with the dialog state
171	}
172
173	// {{{ load() methods
174	private void load(Object obj)
175	{
176		if(obj instanceof OptionGroup)
177		{
178			OptionGroup grp = (OptionGroup)obj;
179			Enumeration members = grp.getMembers();
180			while(members.hasMoreElements())
181			{
182				load(members.nextElement());
183			}
184		}
185	} 
186	
187	protected void load() {
188		load(getDefaultGroup());
189	} // }}}
190	
191	// {{{ getDefaultGroup() method
192	protected OptionGroup getDefaultGroup()
193	{
194		return (OptionGroup) paneTreeModel.getRoot();
195	} // }}}
196
197	@Override
198	public void cancel() {
199		// Clear the temporary mode data in the panes
200		for (int i = 0; i < panes.size(); i++)
201			panes.get(i).cancel();
202		super.cancel();
203	}
204
205} // }}}
206