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

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

fix #15267 - fix various usability problems with new download dialog

  • Property svn:eol-style set to native
File size: 25.5 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.getAllPanels().forEach(AbstractDownloadSourcePanel::rememberSettings);
368 downloadSourcesTab.getSelectedPanel().ifPresent(panel -> DOWNLOAD_SOURCE_TAB.put(panel.getSimpleName()));
369 DOWNLOAD_NEWLAYER.put(cbNewLayer.isSelected());
370 DOWNLOAD_ZOOMTODATA.put(cbZoomToDownloadedData.isSelected());
371 if (currentBounds != null) {
372 Config.getPref().put("osm-download.bounds", currentBounds.encodeAsString(";"));
373 }
374 }
375
376 /**
377 * Restores the previous settings in the download dialog.
378 */
379 public void restoreSettings() {
380 cbNewLayer.setSelected(DOWNLOAD_NEWLAYER.get());
381 cbStartup.setSelected(isAutorunEnabled());
382 cbZoomToDownloadedData.setSelected(DOWNLOAD_ZOOMTODATA.get());
383
384 try {
385 tpDownloadAreaSelectors.setSelectedIndex(DOWNLOAD_TAB.get());
386 } catch (IndexOutOfBoundsException e) {
387 Logging.trace(e);
388 tpDownloadAreaSelectors.setSelectedIndex(0);
389 }
390
391 downloadSourcesTab.getAllPanels().forEach(AbstractDownloadSourcePanel::restoreSettings);
392 downloadSourcesTab.setSelected(DOWNLOAD_SOURCE_TAB.get());
393
394 if (MainApplication.isDisplayingMapView()) {
395 MapView mv = MainApplication.getMap().mapView;
396 currentBounds = new Bounds(
397 mv.getLatLon(0, mv.getHeight()),
398 mv.getLatLon(mv.getWidth(), 0)
399 );
400 boundingBoxChanged(currentBounds, null);
401 } else {
402 Bounds bounds = getSavedDownloadBounds();
403 if (bounds != null) {
404 currentBounds = bounds;
405 boundingBoxChanged(currentBounds, null);
406 }
407 }
408 }
409
410 /**
411 * Returns the previously saved bounding box from preferences.
412 * @return The bounding box saved in preferences if any, {@code null} otherwise.
413 * @since 6509
414 */
415 public static Bounds getSavedDownloadBounds() {
416 String value = Config.getPref().get("osm-download.bounds");
417 if (!value.isEmpty()) {
418 try {
419 return new Bounds(value, ";");
420 } catch (IllegalArgumentException e) {
421 Logging.warn(e);
422 }
423 }
424 return null;
425 }
426
427 /**
428 * Automatically opens the download dialog, if autorun is enabled.
429 * @see #isAutorunEnabled
430 */
431 public static void autostartIfNeeded() {
432 if (isAutorunEnabled()) {
433 MainApplication.getMenu().download.actionPerformed(null);
434 }
435 }
436
437 /**
438 * Returns an {@link Optional} of the currently selected download area.
439 * @return An {@link Optional} of the currently selected download area.
440 * @since 12574 Return type changed to optional
441 */
442 public Optional<Bounds> getSelectedDownloadArea() {
443 return Optional.ofNullable(currentBounds);
444 }
445
446 @Override
447 public void setVisible(boolean visible) {
448 if (visible) {
449 new WindowGeometry(
450 getClass().getName() + ".geometry",
451 WindowGeometry.centerInWindow(
452 getParent(),
453 new Dimension(1000, 600)
454 )
455 ).applySafe(this);
456 } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
457 new WindowGeometry(this).remember(getClass().getName() + ".geometry");
458 }
459 super.setVisible(visible);
460 }
461
462 /**
463 * Replies true if the dialog was canceled
464 *
465 * @return true if the dialog was canceled
466 */
467 public boolean isCanceled() {
468 return canceled;
469 }
470
471 /**
472 * Gets the global settings of the download dialog.
473 * @return The {@link DownloadSettings} object that describes the current state of
474 * the download dialog.
475 */
476 public DownloadSettings getDownloadSettings() {
477 return new DownloadSettings(currentBounds, isNewLayerRequired(), isZoomToDownloadedDataRequired());
478 }
479
480 protected void setCanceled(boolean canceled) {
481 this.canceled = canceled;
482 }
483
484 /**
485 * Adds the download source to the download sources tab.
486 * @param downloadSource The download source to be added.
487 * @param <T> The type of the download data.
488 */
489 protected <T> void addNewDownloadSourceTab(DownloadSource<T> downloadSource) {
490 downloadSourcesTab.addPanel(downloadSource.createPanel(this));
491 }
492
493 /**
494 * Creates listener that removes/adds download sources from/to {@code downloadSourcesTab}
495 * depending on the current mode.
496 * @return The expert mode listener.
497 */
498 private ExpertToggleAction.ExpertModeChangeListener getExpertModeListenerForDownloadSources() {
499 return downloadSourcesTab::updateExpert;
500 }
501
502 /**
503 * Creates a listener that reacts on tab switches for {@code downloadSourcesTab} in order
504 * to adjust proper division of the dialog according to user saved preferences or minimal size
505 * of the panel.
506 * @return A listener to adjust dialog division.
507 */
508 private ChangeListener getDownloadSourceTabChangeListener() {
509 return ec -> downloadSourcesTab.getSelectedPanel().ifPresent(
510 panel -> dialogSplit.setPolicy(panel.getSizingPolicy()));
511 }
512
513 /**
514 * Action that is executed when the cancel button is pressed.
515 */
516 class CancelAction extends AbstractAction {
517 CancelAction() {
518 putValue(NAME, tr("Cancel"));
519 new ImageProvider("cancel").getResource().attachImageIcon(this);
520 putValue(SHORT_DESCRIPTION, tr("Click to close the dialog and to abort downloading"));
521 }
522
523 /**
524 * Cancels the download
525 */
526 public void run() {
527 rememberSettings();
528 setCanceled(true);
529 setVisible(false);
530 }
531
532 @Override
533 public void actionPerformed(ActionEvent e) {
534 Optional<AbstractDownloadSourcePanel<?>> panel = downloadSourcesTab.getSelectedPanel();
535 run();
536 panel.ifPresent(AbstractDownloadSourcePanel::checkCancel);
537 }
538 }
539
540 /**
541 * Action that is executed when the download button is pressed.
542 */
543 class DownloadAction extends AbstractAction {
544 DownloadAction() {
545 putValue(NAME, tr("Download"));
546 new ImageProvider("download").getResource().attachImageIcon(this);
547 putValue(SHORT_DESCRIPTION, tr("Click to download the currently selected area"));
548 setEnabled(!Main.isOffline(OnlineResource.OSM_API));
549 }
550
551 /**
552 * Starts the download and closes the dialog, if all requirements for the current download source are met.
553 * Otherwise the download is not started and the dialog remains visible.
554 */
555 public void run() {
556 rememberSettings();
557 downloadSourcesTab.getSelectedPanel().ifPresent(panel -> {
558 DownloadSettings downloadSettings = getDownloadSettings();
559 if (panel.checkDownload(downloadSettings)) {
560 setCanceled(false);
561 setVisible(false);
562 panel.triggerDownload(downloadSettings);
563 }
564 });
565 }
566
567 @Override
568 public void actionPerformed(ActionEvent e) {
569 run();
570 }
571 }
572
573 class WindowEventHandler extends WindowAdapter {
574 @Override
575 public void windowClosing(WindowEvent e) {
576 new CancelAction().run();
577 }
578
579 @Override
580 public void windowActivated(WindowEvent e) {
581 btnDownload.requestFocusInWindow();
582 }
583 }
584
585 /**
586 * A special tabbed pane for {@link AbstractDownloadSourcePanel}s
587 * @author Michael Zangl
588 * @since 12706
589 */
590 private class DownloadSourceTabs extends JTabbedPane implements DownloadSourceListener {
591 private final List<AbstractDownloadSourcePanel<?>> allPanels = new ArrayList<>();
592
593 DownloadSourceTabs() {
594 downloadSources.forEach(this::downloadSourceAdded);
595 downloadSourcesListeners.addListener(this);
596 }
597
598 List<AbstractDownloadSourcePanel<?>> getAllPanels() {
599 return allPanels;
600 }
601
602 List<AbstractDownloadSourcePanel<?>> getVisiblePanels() {
603 return IntStream.range(0, getTabCount())
604 .mapToObj(this::getComponentAt)
605 .map(p -> (AbstractDownloadSourcePanel<?>) p)
606 .collect(Collectors.toList());
607 }
608
609 void setSelected(String simpleName) {
610 getVisiblePanels().stream()
611 .filter(panel -> simpleName.equals(panel.getSimpleName()))
612 .findFirst()
613 .ifPresent(this::setSelectedComponent);
614 }
615
616 void updateExpert(boolean isExpert) {
617 updateTabs();
618 }
619
620 void addPanel(AbstractDownloadSourcePanel<?> panel) {
621 allPanels.add(panel);
622 updateTabs();
623 }
624
625 private void updateTabs() {
626 // Not the best performance, but we don't do it often
627 removeAll();
628
629 boolean isExpert = ExpertToggleAction.isExpert();
630 allPanels.stream()
631 .filter(panel -> isExpert || !panel.getDownloadSource().onlyExpert())
632 .forEach(panel -> addTab(panel.getDownloadSource().getLabel(), panel.getIcon(), panel));
633 }
634
635 Optional<AbstractDownloadSourcePanel<?>> getSelectedPanel() {
636 return Optional.ofNullable((AbstractDownloadSourcePanel<?>) getSelectedComponent());
637 }
638
639 @Override
640 public void insertTab(String title, Icon icon, Component component, String tip, int index) {
641 if (!(component instanceof AbstractDownloadSourcePanel)) {
642 throw new IllegalArgumentException("Can only add AbstractDownloadSourcePanels");
643 }
644 super.insertTab(title, icon, component, tip, index);
645 }
646
647 @Override
648 public void downloadSourceAdded(DownloadSource<?> source) {
649 addPanel(source.createPanel(DownloadDialog.this));
650 }
651 }
652
653 /**
654 * A special split pane that acts according to a {@link DownloadSourceSizingPolicy}
655 *
656 * It attempts to size the top tab content correctly.
657 *
658 * @author Michael Zangl
659 * @since 12705
660 */
661 private static class DownloadDialogSplitPane extends JSplitPane {
662 private DownloadSourceSizingPolicy policy;
663 private final JTabbedPane topComponent;
664
665 DownloadDialogSplitPane(JTabbedPane newTopComponent, Component newBottomComponent) {
666 super(VERTICAL_SPLIT, newTopComponent, newBottomComponent);
667 this.topComponent = newTopComponent;
668 }
669
670 public void setPolicy(DownloadSourceSizingPolicy policy) {
671 this.policy = policy;
672
673 super.setDividerLocation(policy.getComponentHeight() + computeOffset());
674 setDividerSize(policy.isHeightAdjustable() ? 10 : 0);
675 setEnabled(policy.isHeightAdjustable());
676 }
677
678 @Override
679 public void doLayout() {
680 // We need to force this height before the layout manager is run.
681 // We cannot do this in the setDividerLocation, since the offset cannot be computed there.
682 int offset = computeOffset();
683 if (policy.isHeightAdjustable()) {
684 policy.storeHeight(Math.max(getDividerLocation() - offset, 0));
685 }
686 super.setDividerLocation(policy.getComponentHeight() + offset);
687 super.doLayout();
688 }
689
690 /**
691 * @return The difference between the content height and the divider location
692 */
693 private int computeOffset() {
694 Component selectedComponent = topComponent.getSelectedComponent();
695 return topComponent.getHeight() - (selectedComponent == null ? 0 : selectedComponent.getHeight());
696 }
697 }
698}
Note: See TracBrowser for help on using the repository browser.