Ignore:
Timestamp:
2013-03-22T22:18:26+01:00 (13 years ago)
Author:
zverik
Message:

ImageryID refactoring and javadocs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/OffsetDialog.java

    r29382 r29384  
    55import java.awt.event.ActionListener;
    66import java.awt.event.KeyEvent;
     7import java.io.IOException;
     8import java.net.HttpURLConnection;
     9import java.net.URL;
    710import java.util.*;
    811import java.util.List;
     
    1720import org.openstreetmap.josm.gui.layer.ImageryLayer;
    1821import org.openstreetmap.josm.gui.layer.MapViewPaintable;
     22import org.openstreetmap.josm.tools.*;
    1923import static org.openstreetmap.josm.tools.I18n.tr;
    20 import org.openstreetmap.josm.tools.ImageProvider;
    21 import org.openstreetmap.josm.tools.OpenBrowser;
    2224
    2325/**
    2426 * The dialog which presents a choice between imagery align options.
    2527 *
    26  * @author zverik
     28 * @author Zverik
     29 * @license WTFPL
    2730 */
    2831public class OffsetDialog extends JDialog implements ActionListener, NavigatableComponent.ZoomChangeListener, MapViewPaintable {
     
    3033    protected static final String PREF_DEPRECATED = "iodb.show.deprecated";
    3134    private static final int MAX_OFFSETS = Main.main.pref.getInteger("iodb.max.offsets", 5);
    32     private static final boolean MODAL = false; // modal does not work for executing actions
     35
     36    /**
     37     * Whether to create a modal frame. It turns out, modal dialogs
     38     * block swing worker thread, so offset deprecation, for example, takes
     39     * place only after the dialog is closed. Very inconvenient.
     40     */
     41    private static final boolean MODAL = false;
    3342
    3443    private List<ImageryOffsetBase> offsets;
     
    3645    private JPanel buttonPanel;
    3746
     47    /**
     48     * Initialize the dialog and install listeners.
     49     * @param offsets The list of offset to choose from.
     50     */
    3851    public OffsetDialog( List<ImageryOffsetBase> offsets ) {
    3952        super(JOptionPane.getFrameForComponent(Main.parent), ImageryOffsetTools.DIALOG_TITLE,
     
    5063    }
    5164   
     65    /**
     66     * Creates the GUI.
     67     */
    5268    private void prepareDialog() {
    5369        updateButtonPanel();
     
    89105    }
    90106
     107    /**
     108     * As the name states, this method updates the button panel. It is called
     109     * when a user clicks filtering checkboxes or deprecates an offset.
     110     */
    91111    private void updateButtonPanel() {
    92112        List<ImageryOffsetBase> filteredOffsets = filterOffsets();
     
    112132    }
    113133
     134    /**
     135     * Make a filtered offset list out of the full one. Takes into
     136     * account both checkboxes.
     137     */
    114138    private List<ImageryOffsetBase> filterOffsets() {
    115139        boolean showCalibration = Main.pref.getBoolean(PREF_CALIBRATION, true);
     
    128152    }
    129153
     154    /**
     155     * This listener method is called when a user pans or zooms the map.
     156     * It does nothing, only passes the event to all displayed offset buttons.
     157     */
    130158    public void zoomChanged() {
    131159        for( Component c : buttonPanel.getComponents() ) {
     
    136164    }
    137165
     166    /**
     167     * Draw dots on the map where offsets are located. I doubt it has practical
     168     * value, but looks nice.
     169     */
    138170    public void paint( Graphics2D g, MapView mv, Bounds bbox ) {
    139171        if( offsets == null )
     
    152184    }
    153185   
     186    /**
     187     * Display the dialog and get the return value is case of a modal frame.
     188     * Creates GUI, install a temporary map layer (see {@link #paint} and
     189     * shows the window.
     190     * @return Null for a non-modal dialog, the selected offset
     191     * (or, again, a null value) otherwise.
     192     */
    154193    public ImageryOffsetBase showDialog() {
    155194        // todo: add a temporary layer showing all offsets
     
    164203    }
    165204
     205    /**
     206     * This is a listener method for all buttons (except "Help").
     207     * It assigns a selected offset value and closes the dialog.
     208     * If the dialog wasn't modal, it applies the offset immediately.
     209     * Should it apply the offset either way? Probably.
     210     * @see #applyOffset()
     211     */
     212    public void actionPerformed( ActionEvent e ) {
     213        if( e.getSource() instanceof OffsetDialogButton ) {
     214            selectedOffset = ((OffsetDialogButton)e.getSource()).getOffset();
     215        } else
     216            selectedOffset = null;
     217        NavigatableComponent.removeZoomChangeListener(this);
     218        setVisible(false);
     219        if( !MODAL ) {
     220            Main.map.mapView.removeTemporaryLayer(this);
     221            Main.map.mapView.repaint();
     222            if( selectedOffset != null )
     223                applyOffset();
     224        }
     225    }
     226
     227
     228    /**
     229     * Either applies imagery offset or adds a calibration geometry layer.
     230     * If the offset for each type was chosen for the first time ever,
     231     * it displays an informational message.
     232     */
    166233    public void applyOffset() {
    167234        if( selectedOffset instanceof ImageryOffset ) {
     
    191258    }
    192259
    193     public void actionPerformed( ActionEvent e ) {
    194         if( e.getSource() instanceof OffsetDialogButton ) {
    195             selectedOffset = ((OffsetDialogButton)e.getSource()).getOffset();
    196         } else
    197             selectedOffset = null;
    198         NavigatableComponent.removeZoomChangeListener(this);
    199         setVisible(false);
    200         if( !MODAL ) {
    201             Main.map.mapView.removeTemporaryLayer(this);
    202             Main.map.mapView.repaint();
    203             if( selectedOffset != null )
    204                 applyOffset();
    205         }
    206     }
    207 
     260    /**
     261     * A lisntener for successful deprecations.
     262     */
    208263    private class DeprecateOffsetListener implements QuerySuccessListener {
    209264        ImageryOffsetBase offset;
    210265
     266        /**
     267         * Initialize the listener with an offset.
     268         */
    211269        public DeprecateOffsetListener( ImageryOffsetBase offset ) {
    212270            this.offset = offset;
    213271        }
    214272
     273        /**
     274         * Remove the deprecated offset from the offsets list. Then rebuild the button panel.
     275         */
    215276        public void queryPassed() {
    216277            offset.setDeprecated(new Date(), JosmUserIdentityManager.getInstance().getUserName(), "");
     
    219280    }
    220281
     282    /**
     283     * Opens a web browser with the wiki page in user's language.
     284     */
    221285    class HelpAction extends AbstractAction {
    222286
     
    227291
    228292        public void actionPerformed( ActionEvent e ) {
    229             String base = "http://wiki.openstreetmap.org/wiki/";
     293            String base = Main.pref.get("url.openstreetmap-wiki", "http://wiki.openstreetmap.org/wiki/");
     294            String lang = LanguageInfo.getWikiLanguagePrefix();
    230295            String page = "Imagery_Offset_Database";
    231             String lang = "RU:"; // todo: determine it
     296            try {
     297                // this logic was snatched from {@link org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog.HelpAction}
     298                HttpURLConnection conn = Utils.openHttpConnection(new URL(base + lang + page));
     299                conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect", 10) * 1000);
     300                if( conn.getResponseCode() != 200 ) {
     301                    conn.disconnect();
     302                    lang = "";
     303                }
     304            } catch( IOException ex ) {
     305                lang = "";
     306            }
    232307            OpenBrowser.displayUrl(base + lang + page);
    233308        }
Note: See TracChangeset for help on using the changeset viewer.