images plugin now part of a CE
[fedora-idea.git] / images / src / org / intellij / images / options / impl / OptionsUIForm.java
blob64305b65262a2f5d0b687db46107743c96327d24
1 /*
2 * Copyright 2004-2005 Alexey Efimov
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.intellij.images.options.impl;
18 import com.intellij.openapi.application.Application;
19 import com.intellij.openapi.application.ApplicationManager;
20 import com.intellij.openapi.fileChooser.FileChooser;
21 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
22 import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
23 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
24 import com.intellij.openapi.vfs.LocalFileSystem;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import com.intellij.openapi.util.Computable;
27 import com.intellij.ui.ColorPanel;
28 import com.intellij.ui.DocumentAdapter;
29 import org.intellij.images.ImagesBundle;
30 import org.intellij.images.options.*;
32 import javax.swing.*;
33 import javax.swing.event.ChangeEvent;
34 import javax.swing.event.ChangeListener;
35 import javax.swing.event.DocumentEvent;
36 import javax.swing.text.BadLocationException;
37 import javax.swing.text.Document;
38 import javax.swing.text.Position;
39 import java.awt.*;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.ItemEvent;
43 import java.awt.event.ItemListener;
45 /**
46 * Options UI form bean.
48 * @author <a href="mailto:aefimov.box@gmail.com">Alexey Efimov</a>
50 final class OptionsUIForm {
51 private JPanel contentPane;
52 private JCheckBox showGrid;
53 private JLabel gridLineZoomFactorlLabel;
54 private JSpinner gridLineZoomFactor;
55 private JLabel gridLineSpanLabel;
56 private JSpinner gridLineSpan;
57 private JCheckBox showChessboard;
58 private JSpinner chessboardSize;
59 private JLabel chessboardSizeLabel;
60 private JCheckBox wheelZooming;
61 private JCheckBox smartZooming;
62 private JSpinner smartZoomingWidth;
63 private JLabel smartZoomingWidthLabel;
64 private JSpinner smartZoomingHeight;
65 private JLabel smartZoomingHeightLabel;
66 private JLabel gridLineColorLabel;
67 private ColorPanel gridLineColor;
68 private JLabel chessboardWhiteColorLabel;
69 private JLabel chessboardBlackColorLabel;
70 private ColorPanel chessboardBlackColor;
71 private ColorPanel chessboardWhiteColor;
72 private JLabel externalEditorLabel;
73 private TextFieldWithBrowseButton externalEditorPath;
75 // Options
76 private final Options options = new OptionsImpl();
78 OptionsUIForm() {
79 // Setup labels
80 gridLineZoomFactorlLabel.setLabelFor(gridLineZoomFactor);
81 gridLineSpanLabel.setLabelFor(gridLineSpan);
82 chessboardSizeLabel.setLabelFor(chessboardSize);
83 smartZoomingWidthLabel.setLabelFor(smartZoomingWidth);
84 smartZoomingHeightLabel.setLabelFor(smartZoomingHeight);
85 gridLineColorLabel.setLabelFor(gridLineColor);
86 chessboardWhiteColorLabel.setLabelFor(chessboardWhiteColor);
87 chessboardBlackColorLabel.setLabelFor(chessboardBlackColor);
88 externalEditorLabel.setLabelFor(externalEditorPath);
90 // Setup listeners for enabling and disabling linked checkbox groups
91 smartZooming.addItemListener(new LinkEnabledListener(new JComponent[]{
92 smartZoomingHeightLabel,
93 smartZoomingHeight,
94 smartZoomingWidthLabel,
95 smartZoomingWidth,
96 }));
97 // Setup spinners models
98 gridLineZoomFactor.setModel(new SpinnerNumberModel(GridOptions.DEFAULT_LINE_ZOOM_FACTOR, 2, 8, 1));
99 gridLineSpan.setModel(new SpinnerNumberModel(GridOptions.DEFAULT_LINE_SPAN, 1, 100, 1));
100 chessboardSize.setModel(new SpinnerNumberModel(TransparencyChessboardOptions.DEFAULT_CELL_SIZE, 1, 100, 1));
101 smartZoomingWidth.setModel(new SpinnerNumberModel(ZoomOptions.DEFAULT_PREFFERED_SIZE.width, 1, 9999, 1));
102 smartZoomingHeight.setModel(new SpinnerNumberModel(ZoomOptions.DEFAULT_PREFFERED_SIZE.height, 1, 9999, 1));
104 // Setup listeners for chnages
105 showGrid.addItemListener(new CheckboxOptionsListener(GridOptions.ATTR_SHOW_DEFAULT));
106 gridLineZoomFactor.addChangeListener(new SpinnerOptionsListener(GridOptions.ATTR_LINE_ZOOM_FACTOR));
107 gridLineSpan.addChangeListener(new SpinnerOptionsListener(GridOptions.ATTR_LINE_SPAN));
108 showChessboard.addItemListener(new CheckboxOptionsListener(TransparencyChessboardOptions.ATTR_SHOW_DEFAULT));
109 chessboardSize.addChangeListener(new SpinnerOptionsListener(TransparencyChessboardOptions.ATTR_CELL_SIZE));
110 wheelZooming.addItemListener(new CheckboxOptionsListener(ZoomOptions.ATTR_WHEEL_ZOOMING));
111 smartZooming.addItemListener(new CheckboxOptionsListener(ZoomOptions.ATTR_SMART_ZOOMING));
112 smartZoomingWidth.addChangeListener(new SpinnerOptionsListener(ZoomOptions.ATTR_PREFFERED_WIDTH));
113 smartZoomingHeight.addChangeListener(new SpinnerOptionsListener(ZoomOptions.ATTR_PREFFERED_HEIGHT));
114 gridLineColor.addActionListener(new ColorOptionsListener(GridOptions.ATTR_LINE_COLOR));
115 chessboardWhiteColor.addActionListener(new ColorOptionsListener(TransparencyChessboardOptions.ATTR_WHITE_COLOR));
116 chessboardBlackColor.addActionListener(new ColorOptionsListener(TransparencyChessboardOptions.ATTR_BLACK_COLOR));
117 externalEditorPath.getTextField().getDocument().addDocumentListener(new TextDocumentOptionsListener(ExternalEditorOptions.ATTR_EXECUTABLE_PATH));
119 externalEditorPath.addActionListener(new ExternalEditorPathActionListener());
121 updateUI();
124 public JPanel getContentPane() {
125 return contentPane;
128 private static class LinkEnabledListener implements ItemListener {
129 private final JComponent[] children;
131 LinkEnabledListener(JComponent[] children) {
132 this.children = children.clone();
135 public void itemStateChanged(ItemEvent e) {
136 setSelected(e.getStateChange() == ItemEvent.SELECTED);
139 private void setSelected(boolean selected) {
140 for (JComponent component : children) {
141 component.setEnabled(selected);
146 public Options getOptions() {
147 return options;
150 public void updateUI() {
151 // Grid options
152 EditorOptions editorOptions = options.getEditorOptions();
153 ExternalEditorOptions externalEditorOptions = options.getExternalEditorOptions();
155 GridOptions gridOptions = editorOptions.getGridOptions();
156 showGrid.setSelected(gridOptions.isShowDefault());
157 gridLineZoomFactor.setValue(gridOptions.getLineZoomFactor());
158 gridLineSpan.setValue(gridOptions.getLineSpan());
159 gridLineColor.setSelectedColor(gridOptions.getLineColor());
160 TransparencyChessboardOptions transparencyChessboardOptions = editorOptions.getTransparencyChessboardOptions();
161 showChessboard.setSelected(transparencyChessboardOptions.isShowDefault());
162 chessboardSize.setValue(transparencyChessboardOptions.getCellSize());
163 chessboardWhiteColor.setSelectedColor(transparencyChessboardOptions.getWhiteColor());
164 chessboardBlackColor.setSelectedColor(transparencyChessboardOptions.getBlackColor());
165 ZoomOptions zoomOptions = editorOptions.getZoomOptions();
166 wheelZooming.setSelected(zoomOptions.isWheelZooming());
167 smartZooming.setSelected(zoomOptions.isSmartZooming());
168 Dimension prefferedSize = zoomOptions.getPrefferedSize();
169 smartZoomingWidth.setValue(prefferedSize.width);
170 smartZoomingHeight.setValue(prefferedSize.height);
171 externalEditorPath.setText(externalEditorOptions.getExecutablePath());
174 private final class CheckboxOptionsListener implements ItemListener {
175 private final String name;
177 private CheckboxOptionsListener(String name) {
178 this.name = name;
181 @SuppressWarnings({"UnnecessaryBoxing"})
182 public void itemStateChanged(ItemEvent e) {
183 options.setOption(name, Boolean.valueOf(ItemEvent.SELECTED == e.getStateChange()));
187 private final class SpinnerOptionsListener implements ChangeListener {
188 private final String name;
190 private SpinnerOptionsListener(String name) {
191 this.name = name;
194 public void stateChanged(ChangeEvent e) {
195 JSpinner source = (JSpinner)e.getSource();
196 options.setOption(name, source.getValue());
200 private final class ColorOptionsListener implements ActionListener {
201 private final String name;
203 private ColorOptionsListener(String name) {
204 this.name = name;
207 public void actionPerformed(ActionEvent e) {
208 ColorPanel source = (ColorPanel)e.getSource();
209 options.setOption(name, source.getSelectedColor());
213 private final class TextDocumentOptionsListener extends DocumentAdapter {
214 private final String name;
216 public TextDocumentOptionsListener(String name) {
217 this.name = name;
220 protected void textChanged(DocumentEvent documentEvent) {
221 Document document = documentEvent.getDocument();
222 Position startPosition = document.getStartPosition();
223 try {
224 options.setOption(name, document.getText(startPosition.getOffset(), document.getLength()));
225 } catch (BadLocationException e) {
226 // Ignore
231 private final class ExternalEditorPathActionListener implements ActionListener {
232 public void actionPerformed(ActionEvent e) {
233 Application application = ApplicationManager.getApplication();
234 VirtualFile previous = application.runWriteAction(new Computable<VirtualFile>() {
235 public VirtualFile compute() {
236 return LocalFileSystem.getInstance().refreshAndFindFileByPath(externalEditorPath.getText().replace('\\', '/'));
239 FileChooserDescriptor fileDescriptor = FileChooserDescriptorFactory.createSingleLocalFileDescriptor();
240 fileDescriptor.setShowFileSystemRoots(true);
241 fileDescriptor.setTitle(ImagesBundle.message("select.external.executable.title"));
242 fileDescriptor.setDescription(ImagesBundle.message("select.external.executable.message"));
243 VirtualFile[] virtualFiles = FileChooser.chooseFiles(externalEditorPath, fileDescriptor, previous);
245 if (virtualFiles.length > 0) {
246 String path = virtualFiles[0].getPath();
247 externalEditorPath.setText(path);