source: josm/trunk/src/org/openstreetmap/josm/gui/download/DownloadDialog.java@ 12900

Last change on this file since 12900 was 12900, checked in by Don-vip, 7 years ago

fix #15275 - Download dialog does not remember settings

  • Property svn:eol-style set to native
File size: 25.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.download;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.BorderLayout;
8import java.awt.Component;
9import java.awt.Dimension;
10import java.awt.FlowLayout;
11import java.awt.GridBagLayout;
12import java.awt.event.ActionEvent;
13import java.awt.event.InputEvent;
14import java.awt.event.KeyEvent;
15import java.awt.event.WindowAdapter;
16import java.awt.event.WindowEvent;
17import java.util.ArrayList;
18import java.util.List;
19import java.util.Optional;
20import java.util.stream.Collectors;
21import java.util.stream.IntStream;
22
23import javax.swing.AbstractAction;
24import javax.swing.Icon;
25import javax.swing.JButton;
26import javax.swing.JCheckBox;
27import javax.swing.JComponent;
28import javax.swing.JDialog;
29import javax.swing.JLabel;
30import javax.swing.JPanel;
31import javax.swing.JSplitPane;
32import javax.swing.JTabbedPane;
33import javax.swing.KeyStroke;
34import javax.swing.event.ChangeEvent;
35import javax.swing.event.ChangeListener;
36
37import org.openstreetmap.josm.Main;
38import org.openstreetmap.josm.actions.ExpertToggleAction;
39import org.openstreetmap.josm.data.Bounds;
40import org.openstreetmap.josm.data.preferences.BooleanProperty;
41import org.openstreetmap.josm.data.preferences.IntegerProperty;
42import org.openstreetmap.josm.data.preferences.StringProperty;
43import org.openstreetmap.josm.gui.MainApplication;
44import org.openstreetmap.josm.gui.MapView;
45import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
46import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
47import org.openstreetmap.josm.gui.help.HelpUtil;
48import org.openstreetmap.josm.gui.util.GuiHelper;
49import org.openstreetmap.josm.gui.util.WindowGeometry;
50import org.openstreetmap.josm.io.OnlineResource;
51import org.openstreetmap.josm.plugins.PluginHandler;
52import org.openstreetmap.josm.spi.preferences.Config;
53import org.openstreetmap.josm.tools.GBC;
54import org.openstreetmap.josm.tools.ImageProvider;
55import org.openstreetmap.josm.tools.InputMapUtils;
56import org.openstreetmap.josm.tools.JosmRuntimeException;
57import org.openstreetmap.josm.tools.ListenerList;
58import org.openstreetmap.josm.tools.Logging;
59import org.openstreetmap.josm.tools.OsmUrlToBounds;
60
61/**
62 * Dialog displayed to the user to download mapping data.
63 */
64public class DownloadDialog extends JDialog {
65
66 private static final IntegerProperty DOWNLOAD_TAB = new IntegerProperty("download.tab", 0);
67 private static final StringProperty DOWNLOAD_SOURCE_TAB = new StringProperty("download.source.tab", OSMDownloadSource.SIMPLE_NAME);
68 private static final BooleanProperty DOWNLOAD_AUTORUN = new BooleanProperty("download.autorun", false);
69 private static final BooleanProperty DOWNLOAD_NEWLAYER = new BooleanProperty("download.newlayer", false);
70 private static final BooleanProperty DOWNLOAD_ZOOMTODATA = new BooleanProperty("download.zoomtodata", true);
71
72 /** the unique instance of the download dialog */
73 private static DownloadDialog instance;
74
75 /**
76 * Replies the unique instance of the download dialog
77 *
78 * @return the unique instance of the download dialog
79 */
80 public static synchronized DownloadDialog getInstance() {
81 if (instance == null) {
82 instance = new DownloadDialog(Main.parent);
83 }
84 return instance;
85 }
86
87 protected static final ListenerList<DownloadSourceListener> downloadSourcesListeners = ListenerList.create();
88 protected static final List<DownloadSource<?>> downloadSources = new ArrayList<>();
89 static {
90 // add default download sources
91 addDownloadSource(new OSMDownloadSource());
92 addDownloadSource(new OverpassDownloadSource());
93 }
94
95 protected final transient List<DownloadSelection> downloadSelections = new ArrayList<>();
96 protected final JTabbedPane tpDownloadAreaSelectors = new JTabbedPane();
97 protected final DownloadSourceTabs downloadSourcesTab = new DownloadSourceTabs();
98
99 protected JCheckBox cbNewLayer;
100 protected JCheckBox cbStartup;
101 protected JCheckBox cbZoomToDownloadedData;
102 protected SlippyMapChooser slippyMapChooser;
103 protected JPanel mainPanel;
104 protected DownloadDialogSplitPane dialogSplit;
105
106 /*
107 * Keep the reference globally to avoid having it garbage collected
108 */
109 protected final transient ExpertToggleAction.ExpertModeChangeListener expertListener =
110 getExpertModeListenerForDownloadSources();
111 protected transient Bounds currentBounds;
112 protected boolean canceled;
113
114 protected JButton btnDownload;
115 protected JButton btnCancel;
116 protected JButton btnHelp;
117
118 /**
119 * Builds the main panel of the dialog.
120 * @return The panel of the dialog.
121 */
122 protected final JPanel buildMainPanel() {
123 mainPanel = new JPanel(new GridBagLayout());
124
125 // must be created before hook
126 slippyMapChooser = new SlippyMapChooser();
127
128 // predefined download selections
129 downloadSelections.add(slippyMapChooser);
130 downloadSelections.add(new BookmarkSelection());
131 downloadSelections.add(new BoundingBoxSelection());
132 downloadSelections.add(new PlaceSelection());
133 downloadSelections.add(new TileSelection());
134
135 // add selections from plugins
136 PluginHandler.addDownloadSelection(downloadSelections);
137
138 // register all default download selections
139 for (DownloadSelection s : downloadSelections) {
140 s.addGui(this);
141 }
142
143 // allow to collapse the panes, but reserve some space for tabs
144 downloadSourcesTab.setMinimumSize(new Dimension(0, 25));
145 tpDownloadAreaSelectors.setMinimumSize(new Dimension(0, 0));
146
147 dialogSplit = new DownloadDialogSplitPane(
148 downloadSourcesTab,
149 tpDownloadAreaSelectors);
150
151 ChangeListener tabChangedListener = getDownloadSourceTabChangeListener();
152 tabChangedListener.stateChanged(new ChangeEvent(downloadSourcesTab));
153 downloadSourcesTab.addChangeListener(tabChangedListener);
154
155 mainPanel.add(dialogSplit, GBC.eol().fill());
156
157 cbNewLayer = new JCheckBox(tr("Download as new layer"));
158 cbNewLayer.setToolTipText(tr("<html>Select to download data into a new data layer.<br>"
159 +"Unselect to download into the currently active data layer.</html>"));
160
161 cbStartup = new JCheckBox(tr("Open this dialog on startup"));
162 cbStartup.setToolTipText(
163 tr("<html>Autostart ''Download from OSM'' dialog every time JOSM is started.<br>" +
164 "You can open it manually from File menu or toolbar.</html>"));
165 cbStartup.addActionListener(e -> DOWNLOAD_AUTORUN.put(cbStartup.isSelected()));
166
167 cbZoomToDownloadedData = new JCheckBox(tr("Zoom to downloaded data"));
168 cbZoomToDownloadedData.setToolTipText(tr("Select to zoom to entire newly downloaded data."));
169
170 mainPanel.add(cbNewLayer, GBC.std().anchor(GBC.WEST).insets(5, 5, 5, 5));
171 mainPanel.add(cbStartup, GBC.std().anchor(GBC.WEST).insets(15, 5, 5, 5));
172 mainPanel.add(cbZoomToDownloadedData, GBC.std().anchor(GBC.WEST).insets(15, 5, 5, 5));
173
174 ExpertToggleAction.addVisibilitySwitcher(cbZoomToDownloadedData);
175
176 mainPanel.add(new JLabel(), GBC.eol()); // place info label at a new line
177 JLabel infoLabel = new JLabel(
178 tr("Use left click&drag to select area, arrows or right mouse button to scroll map, wheel or +/- to zoom."));
179 mainPanel.add(infoLabel, GBC.eol().anchor(GBC.CENTER).insets(0, 0, 0, 0));
180
181 ExpertToggleAction.addExpertModeChangeListener(isExpert -> infoLabel.setVisible(!isExpert), true);
182
183 return mainPanel;
184 }
185
186 /**
187 * Builds the button pane of the dialog.
188 * @return The button panel of the dialog.
189 */
190 protected final JPanel buildButtonPanel() {
191 btnDownload = new JButton(new DownloadAction());
192 btnCancel = new JButton(new CancelAction());
193 btnHelp = new JButton(
194 new ContextSensitiveHelpAction(getRootPane().getClientProperty("help").toString()));
195
196 JPanel pnl = new JPanel(new FlowLayout());
197
198 pnl.add(btnDownload);
199 pnl.add(btnCancel);
200 pnl.add(btnHelp);
201
202 InputMapUtils.enableEnter(btnDownload);
203 InputMapUtils.enableEnter(btnCancel);
204 InputMapUtils.addEscapeAction(getRootPane(), btnCancel.getAction());
205 InputMapUtils.enableEnter(btnHelp);
206
207 InputMapUtils.addEnterActionWhenAncestor(cbNewLayer, btnDownload.getAction());
208 InputMapUtils.addEnterActionWhenAncestor(cbStartup, btnDownload.getAction());
209 InputMapUtils.addEnterActionWhenAncestor(cbZoomToDownloadedData, btnDownload.getAction());
210
211 return pnl;
212 }
213
214 /**
215 * Constructs a new {@code DownloadDialog}.
216 * @param parent the parent component
217 */
218 public DownloadDialog(Component parent) {
219 this(parent, ht("/Action/Download"));
220 }
221
222 /**
223 * Constructs a new {@code DownloadDialog}.
224 * @param parent the parent component
225 * @param helpTopic the help topic to assign
226 */
227 public DownloadDialog(Component parent, String helpTopic) {
228 super(GuiHelper.getFrameForComponent(parent), tr("Download"), ModalityType.DOCUMENT_MODAL);
229 HelpUtil.setHelpContext(getRootPane(), helpTopic);
230 getContentPane().setLayout(new BorderLayout());
231 getContentPane().add(buildMainPanel(), BorderLayout.CENTER);
232 getContentPane().add(buildButtonPanel(), BorderLayout.SOUTH);
233
234 getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
235 KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK), "checkClipboardContents");
236
237 getRootPane().getActionMap().put("checkClipboardContents", new AbstractAction() {
238 @Override
239 public void actionPerformed(ActionEvent e) {
240 String clip = ClipboardUtils.getClipboardStringContent();
241 if (clip == null) {
242 return;
243 }
244 Bounds b = OsmUrlToBounds.parse(clip);
245 if (b != null) {
246 boundingBoxChanged(new Bounds(b), null);
247 }
248 }
249 });
250 addWindowListener(new WindowEventHandler());
251 ExpertToggleAction.addExpertModeChangeListener(expertListener);
252 restoreSettings();
253
254 // if no bounding box is selected make sure it is still propagated.
255 if (currentBounds == null) {
256 boundingBoxChanged(null, null);
257 }
258 }
259
260 /**
261 * Distributes a "bounding box changed" from one DownloadSelection
262 * object to the others, so they may update or clear their input fields. Also informs
263 * download sources about the change, so they can react on it.
264 * @param b new current bounds
265 *
266 * @param eventSource - the DownloadSelection object that fired this notification.
267 */
268 public void boundingBoxChanged(Bounds b, DownloadSelection eventSource) {
269 this.currentBounds = b;
270 for (DownloadSelection s : downloadSelections) {
271 if (s != eventSource) {
272 s.setDownloadArea(currentBounds);
273 }
274 }
275
276 for (AbstractDownloadSourcePanel<?> ds : downloadSourcesTab.getAllPanels()) {
277 ds.boudingBoxChanged(b);
278 }
279 }
280
281 /**
282 * Starts download for the given bounding box
283 * @param b bounding box to download
284 */
285 public void startDownload(Bounds b) {
286 this.currentBounds = b;
287 startDownload();
288 }
289
290 /**
291 * Starts download.
292 */
293 public void startDownload() {
294 btnDownload.doClick();
295 }
296
297 /**
298 * Replies true if the user requires to download into a new layer
299 *
300 * @return true if the user requires to download into a new layer
301 */
302 public boolean isNewLayerRequired() {
303 return cbNewLayer.isSelected();
304 }
305
306 /**
307 * Replies true if the user requires to zoom to new downloaded data
308 *
309 * @return true if the user requires to zoom to new downloaded data
310 * @since 11658
311 */
312 public boolean isZoomToDownloadedDataRequired() {
313 return cbZoomToDownloadedData.isSelected();
314 }
315
316 /**
317 * Determines if the dialog autorun is enabled in preferences.
318 * @return {@code true} if the download dialog must be open at startup, {@code false} otherwise.
319 */
320 public static boolean isAutorunEnabled() {
321 return DOWNLOAD_AUTORUN.get();
322 }
323
324 /**
325 * Adds a new download area selector to the download dialog.
326 *
327 * @param selector the download are selector.
328 * @param displayName the display name of the selector.
329 */
330 public void addDownloadAreaSelector(JPanel selector, String displayName) {
331 tpDownloadAreaSelectors.add(displayName, selector);
332 }
333
334 /**
335 * Adds a new download source to the download dialog if it is not added.
336 *
337 * @param downloadSource The download source to be added.
338 * @param <T> The type of the download data.
339 * @throws JosmRuntimeException If the download source is already added. Note, download sources are
340 * compared by their reference.
341 * @since 12878
342 */
343 public static <T> void addDownloadSource(DownloadSource<T> downloadSource) {
344 if (downloadSources.contains(downloadSource)) {
345 throw new JosmRuntimeException("The download source you are trying to add already exists.");
346 }
347
348 downloadSources.add(downloadSource);
349 downloadSourcesListeners.fireEvent(l -> l.downloadSourceAdded(downloadSource));
350 }
351
352 /**
353 * Refreshes the tile sources.
354 * @since 6364
355 */
356 public final void refreshTileSources() {
357 if (slippyMapChooser != null) {
358 slippyMapChooser.refreshTileSources();
359 }
360 }
361
362 /**
363 * Remembers the current settings in the download dialog.
364 */
365 public void rememberSettings() {
366 DOWNLOAD_TAB.put(tpDownloadAreaSelectors.getSelectedIndex());
367 downloadSourcesTab.getSelectedPanel().ifPresent(panel -> DOWNLOAD_SOURCE_TAB.put(panel.getSimpleName()));
368 DOWNLOAD_NEWLAYER.put(cbNewLayer.isSelected());
369 DOWNLOAD_ZOOMTODATA.put(cbZoomToDownloadedData.isSelected());
370 if (currentBounds != null) {
371 Config.getPref().put("osm-download.bounds", currentBounds.encodeAsString(";"));
372 }
373 }
374
375 /**
376 * Restores the previous settings in the download dialog.
377 */
378 public void restoreSettings() {
379 cbNewLayer.setSelected(DOWNLOAD_NEWLAYER.get());
380 cbStartup.setSelected(isAutorunEnabled());
381 cbZoomToDownloadedData.setSelected(DOWNLOAD_ZOOMTODATA.get());
382
383 try {
384 tpDownloadAreaSelectors.setSelectedIndex(DOWNLOAD_TAB.get());
385 } catch (IndexOutOfBoundsException e) {
386 Logging.trace(e);
387 tpDownloadAreaSelectors.setSelectedIndex(0);
388 }
389
390 downloadSourcesTab.getAllPanels().forEach(AbstractDownloadSourcePanel::restoreSettings);
391 downloadSourcesTab.setSelected(DOWNLOAD_SOURCE_TAB.get());
392
393 if (MainApplication.isDisplayingMapView()) {
394 MapView mv = MainApplication.getMap().mapView;
395 currentBounds = new Bounds(
396 mv.getLatLon(0, mv.getHeight()),
397 mv.getLatLon(mv.getWidth(), 0)
398 );
399 boundingBoxChanged(currentBounds, null);
400 } else {
401 Bounds bounds = getSavedDownloadBounds();
402 if (bounds != null) {
403 currentBounds = bounds;
404 boundingBoxChanged(currentBounds, null);
405 }
406 }
407 }
408
409 /**
410 * Returns the previously saved bounding box from preferences.
411 * @return The bounding box saved in preferences if any, {@code null} otherwise.
412 * @since 6509
413 */
414 public static Bounds getSavedDownloadBounds() {
415 String value = Config.getPref().get("osm-download.bounds");
416 if (!value.isEmpty()) {
417 try {
418 return new Bounds(value, ";");
419 } catch (IllegalArgumentException e) {
420 Logging.warn(e);
421 }
422 }
423 return null;
424 }
425
426 /**
427 * Automatically opens the download dialog, if autorun is enabled.
428 * @see #isAutorunEnabled
429 */
430 public static void autostartIfNeeded() {
431 if (isAutorunEnabled()) {
432 MainApplication.getMenu().download.actionPerformed(null);
433 }
434 }
435
436 /**
437 * Returns an {@link Optional} of the currently selected download area.
438 * @return An {@link Optional} of the currently selected download area.
439 * @since 12574 Return type changed to optional
440 */
441 public Optional<Bounds> getSelectedDownloadArea() {
442 return Optional.ofNullable(currentBounds);
443 }
444
445 @Override
446 public void setVisible(boolean visible) {
447 if (visible) {
448 new WindowGeometry(
449 getClass().getName() + ".geometry",
450 WindowGeometry.centerInWindow(
451 getParent(),
452 new Dimension(1000, 600)
453 )
454 ).applySafe(this);
455 } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
456 new WindowGeometry(this).remember(getClass().getName() + ".geometry");
457 }
458 super.setVisible(visible);
459 }
460
461 /**
462 * Replies true if the dialog was canceled
463 *
464 * @return true if the dialog was canceled
465 */
466 public boolean isCanceled() {
467 return canceled;
468 }
469
470 /**
471 * Gets the global settings of the download dialog.
472 * @return The {@link DownloadSettings} object that describes the current state of
473 * the download dialog.
474 */
475 public DownloadSettings getDownloadSettings() {
476 return new DownloadSettings(currentBounds, isNewLayerRequired(), isZoomToDownloadedDataRequired());
477 }
478
479 protected void setCanceled(boolean canceled) {
480 this.canceled = canceled;
481 }
482
483 /**
484 * Adds the download source to the download sources tab.
485 * @param downloadSource The download source to be added.
486 * @param <T> The type of the download data.
487 */
488 protected <T> void addNewDownloadSourceTab(DownloadSource<T> downloadSource) {
489 downloadSourcesTab.addPanel(downloadSource.createPanel(this));
490 }
491
492 /**
493 * Creates listener that removes/adds download sources from/to {@code downloadSourcesTab}
494 * depending on the current mode.
495 * @return The expert mode listener.
496 */
497 private ExpertToggleAction.ExpertModeChangeListener getExpertModeListenerForDownloadSources() {
498 return downloadSourcesTab::updateExpert;
499 }
500
501 /**
502 * Creates a listener that reacts on tab switches for {@code downloadSourcesTab} in order
503 * to adjust proper division of the dialog according to user saved preferences or minimal size
504 * of the panel.
505 * @return A listener to adjust dialog division.
506 */
507 private ChangeListener getDownloadSourceTabChangeListener() {
508 return ec -> downloadSourcesTab.getSelectedPanel().ifPresent(
509 panel -> dialogSplit.setPolicy(panel.getSizingPolicy()));
510 }
511
512 /**
513 * Action that is executed when the cancel button is pressed.
514 */
515 class CancelAction extends AbstractAction {
516 CancelAction() {
517 putValue(NAME, tr("Cancel"));
518 new ImageProvider("cancel").getResource().attachImageIcon(this);
519 putValue(SHORT_DESCRIPTION, tr("Click to close the dialog and to abort downloading"));
520 }
521
522 /**
523 * Cancels the download
524 */
525 public void run() {
526 rememberSettings();
527 setCanceled(true);
528 setVisible(false);
529 }
530
531 @Override
532 public void actionPerformed(ActionEvent e) {
533 Optional<AbstractDownloadSourcePanel<?>> panel = downloadSourcesTab.getSelectedPanel();
534 run();
535 panel.ifPresent(AbstractDownloadSourcePanel::checkCancel);
536 }
537 }
538
539 /**
540 * Action that is executed when the download button is pressed.
541 */
542 class DownloadAction extends AbstractAction {
543 DownloadAction() {
544 putValue(NAME, tr("Download"));
545 new ImageProvider("download").getResource().attachImageIcon(this);
546 putValue(SHORT_DESCRIPTION, tr("Click to download the currently selected area"));
547 setEnabled(!Main.isOffline(OnlineResource.OSM_API));
548 }
549
550 /**
551 * Starts the download and closes the dialog, if all requirements for the current download source are met.
552 * Otherwise the download is not started and the dialog remains visible.
553 */
554 public void run() {
555 rememberSettings();
556 downloadSourcesTab.getSelectedPanel().ifPresent(panel -> {
557 DownloadSettings downloadSettings = getDownloadSettings();
558 if (panel.checkDownload(downloadSettings)) {
559 setCanceled(false);
560 setVisible(false);
561 panel.triggerDownload(downloadSettings);
562 }
563 });
564 }
565
566 @Override
567 public void actionPerformed(ActionEvent e) {
568 run();
569 }
570 }
571
572 class WindowEventHandler extends WindowAdapter {
573 @Override
574 public void windowClosing(WindowEvent e) {
575 new CancelAction().run();
576 }
577
578 @Override
579 public void windowActivated(WindowEvent e) {
580 btnDownload.requestFocusInWindow();
581 }
582 }
583
584 /**
585 * A special tabbed pane for {@link AbstractDownloadSourcePanel}s
586 * @author Michael Zangl
587 * @since 12706
588 */
589 private class DownloadSourceTabs extends JTabbedPane implements DownloadSourceListener {
590 private final List<AbstractDownloadSourcePanel<?>> allPanels = new ArrayList<>();
591
592 DownloadSourceTabs() {
593 downloadSources.forEach(this::downloadSourceAdded);
594 downloadSourcesListeners.addListener(this);
595 }
596
597 List<AbstractDownloadSourcePanel<?>> getAllPanels() {
598 return allPanels;
599 }
600
601 List<AbstractDownloadSourcePanel<?>> getVisiblePanels() {
602 return IntStream.range(0, getTabCount())
603 .mapToObj(this::getComponentAt)
604 .map(p -> (AbstractDownloadSourcePanel<?>) p)
605 .collect(Collectors.toList());
606 }
607
608 void setSelected(String simpleName) {
609 getVisiblePanels().stream()
610 .filter(panel -> simpleName.equals(panel.getSimpleName()))
611 .findFirst()
612 .ifPresent(this::setSelectedComponent);
613 }
614
615 void updateExpert(boolean isExpert) {
616 updateTabs();
617 }
618
619 void addPanel(AbstractDownloadSourcePanel<?> panel) {
620 allPanels.add(panel);
621 updateTabs();
622 }
623
624 private void updateTabs() {
625 // Not the best performance, but we don't do it often
626 removeAll();
627
628 boolean isExpert = ExpertToggleAction.isExpert();
629 allPanels.stream()
630 .filter(panel -> isExpert || !panel.getDownloadSource().onlyExpert())
631 .forEach(panel -> addTab(panel.getDownloadSource().getLabel(), panel.getIcon(), panel));
632 }
633
634 Optional<AbstractDownloadSourcePanel<?>> getSelectedPanel() {
635 return Optional.ofNullable((AbstractDownloadSourcePanel<?>) getSelectedComponent());
636 }
637
638 @Override
639 public void insertTab(String title, Icon icon, Component component, String tip, int index) {
640 if (!(component instanceof AbstractDownloadSourcePanel)) {
641 throw new IllegalArgumentException("Can only add AbstractDownloadSourcePanels");
642 }
643 super.insertTab(title, icon, component, tip, index);
644 }
645
646 @Override
647 public void downloadSourceAdded(DownloadSource<?> source) {
648 addPanel(source.createPanel(DownloadDialog.this));
649 }
650 }
651
652 /**
653 * A special split pane that acts according to a {@link DownloadSourceSizingPolicy}
654 *
655 * It attempts to size the top tab content correctly.
656 *
657 * @author Michael Zangl
658 * @since 12705
659 */
660 private static class DownloadDialogSplitPane extends JSplitPane {
661 private DownloadSourceSizingPolicy policy;
662 private final JTabbedPane topComponent;
663
664 DownloadDialogSplitPane(JTabbedPane newTopComponent, Component newBottomComponent) {
665 super(VERTICAL_SPLIT, newTopComponent, newBottomComponent);
666 this.topComponent = newTopComponent;
667 }
668
669 public void setPolicy(DownloadSourceSizingPolicy policy) {
670 this.policy = policy;
671
672 super.setDividerLocation(policy.getComponentHeight() + computeOffset());
673 setDividerSize(policy.isHeightAdjustable() ? 10 : 0);
674 setEnabled(policy.isHeightAdjustable());
675 }
676
677 @Override
678 public void doLayout() {
679 // We need to force this height before the layout manager is run.
680 // We cannot do this in the setDividerLocation, since the offset cannot be computed there.
681 int offset = computeOffset();
682 if (policy.isHeightAdjustable()) {
683 policy.storeHeight(Math.max(getDividerLocation() - offset, 0));
684 }
685 super.setDividerLocation(policy.getComponentHeight() + offset);
686 super.doLayout();
687 }
688
689 /**
690 * @return The difference between the content height and the divider location
691 */
692 private int computeOffset() {
693 Component selectedComponent = topComponent.getSelectedComponent();
694 return topComponent.getHeight() - (selectedComponent == null ? 0 : selectedComponent.getHeight());
695 }
696 }
697}
Note: See TracBrowser for help on using the repository browser.