content: /*
   * 07/20/2011
   *
   * JspOptionsPanel.java - Options for JSP language support.
   * Copyright (C) 2010 Robert Futrell
   * http://fifesoft.com/rtext
   * Licensed under a modified BSD license.
   * See the included license file for details.
   */
  package ide.plugins.langsupport;
  
  import java.awt.BorderLayout;
  import java.awt.ComponentOrientation;
  import java.awt.Frame;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.ResourceBundle;
  import javax.swing.Box;
  import javax.swing.ImageIcon;
  import javax.swing.JButton;
  import javax.swing.JCheckBox;
  import javax.swing.JComponent;
  import javax.swing.border.Border;
  
  import org.fife.rsta.ac.LanguageSupportFactory;
  import org.fife.rsta.ac.jsp.JspLanguageSupport;
  import ide.AbstractMainView;
  import ide.RText;
  import org.fife.ui.OptionsDialogPanel;
  import org.fife.ui.UIUtil;
  import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
  
  
  
  /**
   * Options panel for JSP code completion.
   *
   * @author Robert Futrell
   * @version 1.0
   */
  class JspOptionsPanel extends OptionsDialogPanel {
  
  	private Listener listener;
  	private JCheckBox enabledCB;
  	private JCheckBox autoAddClosingTagsCB;
  	private JCheckBox foldingEnabledCB;
  	private JButton rdButton;
  
  	private static final String PROPERTY		= "Property";
  
  
  	/**
  	 * Constructor.
  	 */
  	public JspOptionsPanel() {
  
  		ResourceBundle msg = Plugin.msg;
  		setName(msg.getString("Options.Jsp.Name"));
  		listener = new Listener();
  		setIcon(new ImageIcon(getClass().getResource("page_white_code_red.png")));
  
  		ComponentOrientation o = ComponentOrientation.
  											getOrientation(getLocale());
  
  		setLayout(new BorderLayout());
  		Border empty5Border = UIUtil.getEmpty5Border();
  		setBorder(empty5Border);
  
  		Box cp = Box.createVerticalBox();
  		cp.setBorder(null);
  		add(cp, BorderLayout.NORTH);
  
  		Box box = Box.createVerticalBox();
  		box.setBorder(new OptionPanelBorder(msg.
  				getString("Options.General.Section.General")));
  		cp.add(box);
  		cp.add(Box.createVerticalStrut(5));
  
  		enabledCB = createCB("Options.General.EnableCodeCompletion");
  		addLeftAligned(box, enabledCB);
  		cp.add(Box.createVerticalStrut(5));
  
  		autoAddClosingTagsCB = createCB("Options.Html.AutoAddClosingTags");
  		addLeftAligned(box, autoAddClosingTagsCB, 5);
  		cp.add(Box.createVerticalStrut(5));
  
  		foldingEnabledCB = createCB("Options.General.EnableCodeFolding");
  		addLeftAligned(box, foldingEnabledCB, 5);
  		cp.add(Box.createVerticalStrut(5));
  
  		rdButton = new JButton(msg.getString("Options.General.RestoreDefaults"));
  		rdButton.addActionListener(listener);
  		addLeftAligned(cp, rdButton);
  
  		cp.add(Box.createVerticalGlue());
  
  		applyComponentOrientation(o);
  
  	}
  
  
  	private JCheckBox createCB(String key) {
  		if (key.indexOf('.')==-1) {
  			key = "Options.JSP." + key;
  		}
  		JCheckBox cb = new JCheckBox(Plugin.msg.getString(key));
  		cb.addActionListener(listener);
  		return cb;
  	}
  
  
  	/**
  	 * {@inheritDoc}
  	 */
  	@Override
  	protected void doApplyImpl(Frame owner) {
  
  		LanguageSupportFactory lsf = LanguageSupportFactory.get();
  		JspLanguageSupport ls = (JspLanguageSupport)lsf.getSupportFor(
  				SyntaxConstants.SYNTAX_STYLE_JSP);
  
  		// HTML-specific options
  		ls.setAutoAddClosingTags(autoAddClosingTagsCB.isSelected());
  
  		// Options dealing with code completion.
  		ls.setAutoCompleteEnabled(enabledCB.isSelected());
  
  		// Options dealing with code folding.
  		RText rtext = (RText)owner;
  		AbstractMainView view = rtext.getMainView();
  		boolean folding = foldingEnabledCB.isSelected();
  		view.setCodeFoldingEnabledFor(SyntaxConstants.SYNTAX_STYLE_JSP, folding);
  
  	}
  
  
  	/**
  	 * {@inheritDoc}
  	 */
  	@Override
  	protected OptionsPanelCheckResult ensureValidInputsImpl() {
  		return null;
  	}
  
  
  	/**
  	 * {@inheritDoc}
  	 */
  	@Override
  	public JComponent getTopJComponent() {
  		return enabledCB;
  	}
  
  
  	private void setEnabledCBSelected(boolean selected) {
  		enabledCB.setSelected(selected);
  		// TODO: Toggle enabled state of all other components.
  	}
  
  
  	/**
  	 * {@inheritDoc}
  	 */
  	@Override
  	protected void setValuesImpl(Frame owner) {
  
  		LanguageSupportFactory lsf = LanguageSupportFactory.get();
  		JspLanguageSupport ls = (JspLanguageSupport)lsf.getSupportFor(
  				SyntaxConstants.SYNTAX_STYLE_JSP);
  
  		// HTML-specific options
  		autoAddClosingTagsCB.setSelected(ls.getAutoAddClosingTags());
  
  		// Options dealing with code completion
  		setEnabledCBSelected(ls.isAutoCompleteEnabled());
  
  		// Options related to code folding.
  		RText rtext = (RText)owner;
  		AbstractMainView view = rtext.getMainView();
  		boolean folding = view.isCodeFoldingEnabledFor(SyntaxConstants.SYNTAX_STYLE_JSP);
  		foldingEnabledCB.setSelected(folding);
  
  	}
  
  
  	/**
  	 * Listens for events in this options panel.
  	 */
  	private class Listener implements ActionListener {
  
  		@Override
  		public void actionPerformed(ActionEvent e) {
  
  			Object source = e.getSource();
  
  			if (enabledCB==source) {
  				// Trick related components to toggle enabled states
  				setEnabledCBSelected(enabledCB.isSelected());
  				hasUnsavedChanges = true;
  				firePropertyChange(PROPERTY, null, null);
  			}
  
  			else if (autoAddClosingTagsCB==source) {
  				hasUnsavedChanges = true;
  				firePropertyChange(PROPERTY, null, null);
  			}
  
  			else if (foldingEnabledCB==source) {
  				hasUnsavedChanges = true;
  				firePropertyChange(PROPERTY, null, null);
  			}
  
  			else if (rdButton==source) {
  				if (!enabledCB.isSelected() ||
  						!autoAddClosingTagsCB.isSelected() ||
  						!foldingEnabledCB.isSelected()) {
  					enabledCB.setSelected(true);
  					autoAddClosingTagsCB.setSelected(true);
  					foldingEnabledCB.setSelected(true);
  					hasUnsavedChanges = true;
  					firePropertyChange(PROPERTY, null, null);
  				}
  			}
  
  		}
  
  	}
  
  
  }