summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/visualization/savedialog/SaveOptionsPanel.java
blob: 6193310bd592b2fca3152563a1a7a5b8c7cefe69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package de.lmu.ifi.dbs.elki.visualization.savedialog;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2012
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * A component (JPanel) which can be displayed in the save dialog to show
 * additional options when saving as JPEG or PNG.
 * 
 * @author Simon Mittermüller
 */
public class SaveOptionsPanel extends JPanel {
  // TODO: externalize strings
  private static final String STR_IMAGE_SIZE = "Size options:";

  private static final String STR_JPEG_QUALITY = "Quality:";

  private static final String STR_IMAGE_HEIGHT = "Height:";

  private static final String STR_IMAGE_WIDTH = "Width:";

  private static final String STR_CHOOSE_FORMAT = "Choose format:";

  private static final String STR_RESET_IMAGE_SIZE = "Reset image size";

  private static final String STR_LOCK_ASPECT_RATIO = "Lock aspect ratio";

  /**
   * Serial version.
   */
  private static final long serialVersionUID = 1L;

  /** The fileChooser on which this panel is installed. */
  private JFileChooser fc;

  /**
   * The width of the exported image (if exported to JPEG or PNG). Default is
   * 1024.
   */
  protected int width = 1024;

  /**
   * The height of the exported image (if exported to JPEG or PNG). Default is
   * 768.
   */
  protected int height = 768;

  /** Ratio for easier size adjustment */
  double ratio = 16.0 / 10.0;

  /** Main panel */
  private JPanel mainPanel;

  /** Shows quality info when saving as JPEG. */
  private JPanel qualPanel;

  /** If saving as JPEG/PNG show width/height infos here. */
  private JPanel sizePanel;

  protected JSpinner spinnerWidth;

  protected JSpinner spinnerHeight;

  protected JSpinner spinnerQual;

  protected SpinnerNumberModel modelWidth;

  protected SpinnerNumberModel modelHeight;

  protected SpinnerNumberModel modelQuality;

  protected JCheckBox aspectRatioLock;

  protected JButton resetSizeButton;

  protected JComboBox formatSelector;

  // Not particularly useful for most - hide it for now.
  private final boolean hasResetButton = false;

  /**
   * Construct a new Save Options Panel.
   * 
   * @param fc File chooser to display in
   * @param w Default image width
   * @param h Default image height
   */
  public SaveOptionsPanel(JFileChooser fc, int w, int h) {
    this.width = w;
    this.height = h;
    this.ratio = (double) w / (double) h;
    this.fc = fc;

    mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));

    sizePanel = new JPanel();
    sizePanel.setLayout(new BoxLayout(sizePanel, BoxLayout.Y_AXIS));
    sizePanel.setBorder(BorderFactory.createTitledBorder(STR_IMAGE_SIZE));

    // *** Format panel
    mainPanel.add(new JLabel(STR_CHOOSE_FORMAT));

    formatSelector = new JComboBox(SVGSaveDialog.getVisibleFormats());
    formatSelector.setSelectedIndex(0);
    formatSelector.addItemListener(new ItemListener() {
      @Override
      public void itemStateChanged(ItemEvent e) {
        if(e.getItem() != null) {
          String format = (String) formatSelector.getSelectedItem();
          setFormat(format);
        }
      }
    });
    mainPanel.add(formatSelector);

    // *** Size panel
    JPanel widthPanel = new JPanel();
    JPanel heightPanel = new JPanel();
    widthPanel.add(new JLabel(STR_IMAGE_WIDTH));
    heightPanel.add(new JLabel(STR_IMAGE_HEIGHT));

    // size models
    modelWidth = new SpinnerNumberModel(width, 0, 100000, 1);
    modelHeight = new SpinnerNumberModel(height, 0, 100000, 1);

    // size spinners
    spinnerWidth = new JSpinner(modelWidth);
    spinnerWidth.addChangeListener(new ChangeListener() {
      @Override
      public void stateChanged(ChangeEvent e) {
        if(aspectRatioLock.isSelected()) {
          int val = modelWidth.getNumber().intValue();
          spinnerHeight.setValue(new Integer((int) Math.round(val / ratio)));
        }
      }
    });
    widthPanel.add(spinnerWidth);

    spinnerHeight = new JSpinner(modelHeight);
    spinnerHeight.addChangeListener(new ChangeListener() {
      @Override
      public void stateChanged(ChangeEvent e) {
        if(aspectRatioLock.isSelected()) {
          int val = modelHeight.getNumber().intValue();
          spinnerWidth.setValue(new Integer((int) Math.round(val * ratio)));
        }
      }
    });
    heightPanel.add(spinnerHeight);

    // add subpanels
    sizePanel.add(widthPanel);
    sizePanel.add(heightPanel);

    // aspect lock
    aspectRatioLock = new JCheckBox(STR_LOCK_ASPECT_RATIO);
    aspectRatioLock.setSelected(true);
    // aspectRatioLock.addActionListener(x);
    sizePanel.add(aspectRatioLock);

    // reset size button
    if(hasResetButton) {
      resetSizeButton = new JButton(STR_RESET_IMAGE_SIZE);
      resetSizeButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          modelWidth.setValue(width);
          modelHeight.setValue(height);
          aspectRatioLock.setSelected(true);
        }
      });
      sizePanel.add(resetSizeButton);
    }

    mainPanel.add(sizePanel);

    // Quality settings panel
    qualPanel = new JPanel();
    // quality settings will not be visible by default (JPEG only)
    qualPanel.setVisible(false);
    qualPanel.setLayout(new BoxLayout(qualPanel, BoxLayout.Y_AXIS));
    qualPanel.setBorder(BorderFactory.createTitledBorder(STR_JPEG_QUALITY));
    modelQuality = new SpinnerNumberModel(0.7, 0.1, 1.0, 0.1);
    spinnerQual = new JSpinner(modelQuality);
    // spinnerQual.addChangeListener(x);
    qualPanel.add(spinnerQual);

    mainPanel.add(qualPanel);

    add(mainPanel);

    // setup a listener to react to file name changes
    this.fc.addPropertyChangeListener(new PropertyChangeListener() {
      @Override
      public void propertyChange(PropertyChangeEvent e) {
        if(e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
          File file = (File) e.getNewValue();
          if(file != null && file.getName() != null) {
            String format = SVGSaveDialog.guessFormat(file.getName());
            if(format != null) {
              setFormat(format);
            }
          }
        }
      }
    });
  }

  protected void setFormat(String format) {
    String[] formats = SVGSaveDialog.getVisibleFormats();
    int index = -1;
    for(int i = 0; i < formats.length; i++) {
      if(formats[i].equals(format)) {
        index = i;
        break;
      }
    }
    if(index != formatSelector.getSelectedIndex() && index >= 0) {
      formatSelector.setSelectedIndex(index);
    }
    if(format.equals("jpeg") || format.equals("jpg")) {
      sizePanel.setVisible(true);
      qualPanel.setVisible(true);
    }
    else if(format.equals("png")) {
      sizePanel.setVisible(true);
      qualPanel.setVisible(false);
    }
    else if(format.equals("pdf")) {
      sizePanel.setVisible(false);
      qualPanel.setVisible(false);
      mainPanel.validate();
    }
    else if(format.equals("ps")) {
      sizePanel.setVisible(false);
      qualPanel.setVisible(false);
      mainPanel.validate();
    }
    else if(format.equals("eps")) {
      sizePanel.setVisible(false);
      qualPanel.setVisible(false);
      mainPanel.validate();
    }
    else if(format.equals("svg")) {
      sizePanel.setVisible(false);
      qualPanel.setVisible(false);
      mainPanel.validate();
    }
    else {
      // TODO: what to do on unknown formats?
      // LoggingUtil.warning("Unrecognized file extension seen: " + format);
    }
  }

  /**
   * Return the selected file format.
   * 
   * @return file format identification
   */
  public String getSelectedFormat() {
    String format = (String) formatSelector.getSelectedItem();
    return format;
  }

  /**
   * Returns the quality value in the quality field.
   * 
   * It is ensured that return value is in the range of [0:1]
   * 
   * @return Quality value for JPEG.
   */
  public double getJPEGQuality() {
    Double qual = 0.7;
    qual = modelQuality.getNumber().doubleValue();
    if(qual > 1.0) {
      qual = 1.0;
    }
    if(qual < 0) {
      qual = 0.0;
    }
    return qual;
  }
}