content: /*
   * 05/23/2010
   *
   * HtmlOptionsPanel.java - Options for HTML 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.BorderFactory;
  import javax.swing.Box;
  import javax.swing.ImageIcon;
  import javax.swing.JButton;
  import javax.swing.JCheckBox;
  import javax.swing.JComponent;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  import javax.swing.SpringLayout;
  import javax.swing.border.Border;
  import javax.swing.event.DocumentEvent;
  import javax.swing.event.DocumentListener;
  import javax.swing.text.AbstractDocument;
  
  import org.fife.rsta.ac.LanguageSupport;
  import org.fife.rsta.ac.LanguageSupportFactory;
  import org.fife.rsta.ac.html.HtmlLanguageSupport;
  import ide.AbstractMainView;
  import ide.NumberDocumentFilter;
  import ide.RText;
  import org.fife.ui.OptionsDialogPanel;
  import org.fife.ui.UIUtil;
  import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
  
  
  /**
   * Options panel for HTML code completion.
   *
   * @author Robert Futrell
   * @version 1.0
   */
  class HtmlOptionsPanel extends OptionsDialogPanel {
  
  	private Listener listener;
  	private JCheckBox enabledCB;
  	private JCheckBox showDescWindowCB;
  	private JCheckBox autoAddClosingTagsCB;
  	private JCheckBox autoActivateCB;
  	private JLabel aaDelayLabel;
  	private JTextField aaDelayField;
  	private JLabel aaKeysLabel;
  	private JTextField aaKeysField;
  	private JCheckBox foldingEnabledCB;
  	private JButton rdButton;
  
  	private static final String PROPERTY		= "Property";
  
  
  	/**
  	 * Constructor.
  	 */
  	public HtmlOptionsPanel() {
  
  		ResourceBundle msg = Plugin.msg;
  		setName(msg.getString("Options.Html.Name"));
  		listener = new Listener();
  		setIcon(new ImageIcon(RText.class.getResource("graphics/file_icons/html.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, 5);
  
  		Box box2 = Box.createVerticalBox();
  		if (o.isLeftToRight()) {
  			box2.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
  		}
  		else {
  			box2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));
  		}
  		box.add(box2);
  
  		showDescWindowCB = createCB("Options.General.ShowDescWindow");
  		addLeftAligned(box2, showDescWindowCB, 5);
  
  		autoAddClosingTagsCB = createCB("Options.Html.AutoAddClosingTags");
  		addLeftAligned(box, autoAddClosingTagsCB, 5);
  
  		box2.add(Box.createVerticalGlue());
  
  		box = Box.createVerticalBox();
  		box.setBorder(new OptionPanelBorder(
  				msg.getString("Options.General.AutoActivation")));
  		cp.add(box);
  
  		autoActivateCB = createCB("Options.General.EnableAutoActivation");
  		addLeftAligned(box, autoActivateCB, 5);
  
  		box2 = Box.createVerticalBox();
  		if (o.isLeftToRight()) {
  			box2.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
  		}
  		else {
  			box2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));
  		}
  		box.add(box2);
  
  		SpringLayout sl = new SpringLayout();
  		JPanel temp = new JPanel(sl);
  		aaDelayLabel = new JLabel(msg.getString("Options.General.AutoActivationDelay"));
  		aaDelayField = new JTextField(10);
  		AbstractDocument doc = (AbstractDocument)aaDelayField.getDocument();
  		doc.setDocumentFilter(new NumberDocumentFilter());
  		doc.addDocumentListener(listener);
  		aaKeysLabel = new JLabel(msg.getString("Options.Html.AutoActivationKeys"));
  		aaKeysLabel.setEnabled(false);
  		aaKeysField = new JTextField("<", 10);
  		aaKeysField.setEnabled(false);
  		if (o.isLeftToRight()) {
  			temp.add(aaDelayLabel);		temp.add(aaDelayField);
  			temp.add(aaKeysLabel);		temp.add(aaKeysField);
  		}
  		else {
  			temp.add(aaDelayField);		temp.add(aaDelayLabel);
  			temp.add(aaKeysField);		temp.add(aaKeysLabel);
  		}
  		UIUtil.makeSpringCompactGrid(temp, 2,2, 0,0, 5,5);
  		JPanel temp2 = new JPanel(new BorderLayout());
  		temp2.add(temp, BorderLayout.LINE_START);
  		box2.add(temp2);
  
  		box2.add(Box.createVerticalGlue());
  
  		cp.add(Box.createVerticalStrut(5));
  		box = Box.createVerticalBox();
  		box.setBorder(new OptionPanelBorder(msg.
  				getString("Options.General.Section.Folding")));
  		cp.add(box);
  		cp.add(Box.createVerticalStrut(5));
  
  		foldingEnabledCB = createCB("Options.General.EnableCodeFolding");
  		addLeftAligned(box, foldingEnabledCB, 5);
  
  		rdButton = new JButton(msg.getString("Options.General.RestoreDefaults"));
  		rdButton.addActionListener(listener);
  		addLeftAligned(cp, rdButton, 5);
  
  		cp.add(Box.createVerticalGlue());
  
  		applyComponentOrientation(o);
  
  	}
  
  
  	private JCheckBox createCB(String key) {
  		if (key.indexOf('.')==-1) {
  			key = "Options.Html." + 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();
  		LanguageSupport ls=lsf.getSupportFor(SyntaxConstants.SYNTAX_STYLE_HTML);
  
  		// HTML-specific options
  		HtmlLanguageSupport hls = (HtmlLanguageSupport)ls;
  		hls.setAutoAddClosingTags(autoAddClosingTagsCB.isSelected());
  
  		// Options dealing with code completion.
  		ls.setAutoCompleteEnabled(enabledCB.isSelected());
  		ls.setShowDescWindow(showDescWindowCB.isSelected());
  
  		// Options dealing with auto-activation.
  		ls.setAutoActivationEnabled(autoActivateCB.isSelected());
  		int delay = 300;
  		String temp = aaDelayField.getText();
  		if (temp.length()>0) {
  			try {
  				delay = Integer.parseInt(aaDelayField.getText());
  			} catch (NumberFormatException nfe) { // Never happens
  				nfe.printStackTrace();
  			}
  		}
  		ls.setAutoActivationDelay(delay);
  
  		RText rtext = (RText)owner;
  		AbstractMainView view = rtext.getMainView();
  		boolean folding = foldingEnabledCB.isSelected();
  		view.setCodeFoldingEnabledFor(SyntaxConstants.SYNTAX_STYLE_HTML, folding);
  
  	}
  
  
  	/**
  	 * {@inheritDoc}
  	 */
  	@Override
  	protected OptionsPanelCheckResult ensureValidInputsImpl() {
  		return null;
  	}
  
  
  	/**
  	 * {@inheritDoc}
  	 */
  	@Override
  	public JComponent getTopJComponent() {
  		return enabledCB;
  	}
  
  
  	private void setAutoActivateCBSelected(boolean selected) {
  		autoActivateCB.setSelected(selected);
  		aaDelayLabel.setEnabled(selected);
  		aaDelayField.setEnabled(selected);
  		//aaKeysLabel.setEnabled(selected);
  		//aaKeysField.setEnabled(selected);
  	}
  
  
  	private void setEnabledCBSelected(boolean selected) {
  		enabledCB.setSelected(selected);
  		showDescWindowCB.setEnabled(selected);
  	}
  
  
  	/**
  	 * {@inheritDoc}
  	 */
  	@Override
  	protected void setValuesImpl(Frame owner) {
  
  		LanguageSupportFactory lsf = LanguageSupportFactory.get();
  		LanguageSupport ls=lsf.getSupportFor(SyntaxConstants.SYNTAX_STYLE_HTML);
  
  		// HTML-specific options
  		HtmlLanguageSupport hls = (HtmlLanguageSupport)ls;
  		autoAddClosingTagsCB.setSelected(hls.getAutoAddClosingTags());
  
  		// Options dealing with code completion
  		setEnabledCBSelected(ls.isAutoCompleteEnabled());
  		showDescWindowCB.setSelected(ls.getShowDescWindow());
  
  		// Options dealing with auto-activation
  		setAutoActivateCBSelected(ls.isAutoActivationEnabled());
  		aaDelayField.setText(Integer.toString(ls.getAutoActivationDelay()));
  
  		// Options related to code folding.
  		RText rtext = (RText)owner;
  		AbstractMainView view = rtext.getMainView();
  		boolean folding = view.isCodeFoldingEnabledFor(SyntaxConstants.SYNTAX_STYLE_HTML);
  		foldingEnabledCB.setSelected(folding);
  
  	}
  
  
  	/**
  	 * Listens for events in this options panel.
  	 */
  	private class Listener implements ActionListener, DocumentListener {
  
  		@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 (autoActivateCB==source) {
  				setAutoActivateCBSelected(autoActivateCB.isSelected());
  				hasUnsavedChanges = true;
  				firePropertyChange(PROPERTY, null, null);
  			}
  
  			else if (foldingEnabledCB==source ||
  					showDescWindowCB==source) {
  				hasUnsavedChanges = true;
  				firePropertyChange(PROPERTY, null, null);
  			}
  
  			else if (rdButton==source) {
  				if (!enabledCB.isSelected() ||
  						!autoAddClosingTagsCB.isSelected() ||
  						!showDescWindowCB.isSelected() ||
  						!autoActivateCB.isSelected() ||
  						!"300".equals(aaDelayField.getText()) ||
  						!foldingEnabledCB.isSelected()) {
  					setEnabledCBSelected(true);
  					autoAddClosingTagsCB.setSelected(true);
  					showDescWindowCB.setSelected(true);
  					setAutoActivateCBSelected(true);
  					aaDelayField.setText("300");
  					foldingEnabledCB.setSelected(true);
  					hasUnsavedChanges = true;
  					firePropertyChange(PROPERTY, null, null);
  				}
  			}
  
  		}
  
  		@Override
  		public void changedUpdate(DocumentEvent e) {
  			handleDocumentEvent(e);
  		}
  
  		private void handleDocumentEvent(DocumentEvent e) {
  			hasUnsavedChanges = true;
  			firePropertyChange(PROPERTY, null, null);
  		}
  
  		@Override
  		public void insertUpdate(DocumentEvent e) {
  			handleDocumentEvent(e);
  		}
  
  		@Override
  		public void removeUpdate(DocumentEvent e) {
  			handleDocumentEvent(e);
  		}
  
  	}
  
  
  }