Changeset 15828 in osm for applications/editors/josm


Ignore:
Timestamp:
2009-06-10T16:30:04+02:00 (16 years ago)
Author:
stoecker
Message:

fix #2275 - patch by xeen - resizable dialog

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/DirectUpload/src/org/openstreetmap/josm/plugins/DirectUpload/UploadDataGui.java

    r14856 r15828  
    6060
    6161    // Fields are declared here for easy access
    62     private JMultilineLabel OutputDisplay = new JMultilineLabel("");
    63     private JTextField descriptionField = new JTextField();
    64     private JTextField tagsField = new JTextField();
     62    // Do not remove the space in JMultilineLabel. Otherwise the label will be empty
     63    // as we don't know its contents yet and therefore have a height of 0. This will
     64    // lead to unnecessary scrollbars.
     65    private JMultilineLabel OutputDisplay = new JMultilineLabel(" ");
     66    private JTextField descriptionField = new JTextField(50);
     67    private JTextField tagsField = new JTextField(50);
    6568    private JCheckBox publicCheckbox = new JCheckBox();
    66    
     69
    6770    // Constants used when generating upload request
    6871    private static final String API_VERSION = "0.6";
     
    7477    private String datename = new SimpleDateFormat("yyMMddHHmmss").format(new Date());
    7578    private String filename = "";
    76    
     79
    7780    private boolean cancelled = false;
    7881
     
    8689        JPanel content = initComponents();
    8790        autoSelectTrace();
    88        
    89         contentConstraints = GBC.eol().fill().insets(5,10,5,0);
     91
    9092        setupDialog(content, new String[] { "uploadtrace.png", "cancel.png" });
    91        
     93
    9294        buttons.get(0).setEnabled(!checkForGPXLayer());
    93        
    94         setSize(findMaxDialogSize());
    95     }
    96    
     95    }
     96
    9797    /**
    9898     * Sets up the dialog window elements
     
    109109        tagsField.setToolTipText(tr("Please enter tags about your trace."));
    110110
    111         JPanel p = new JPanel();
    112         p.setLayout(new GridBagLayout());
     111        JPanel p = new JPanel(new GridBagLayout());
    113112
    114113        OutputDisplay.setMaxWidth(findMaxDialogSize().width-10);
     
    125124        return p;
    126125    }
    127    
     126
    128127    /**
    129128     * This function will automatically select a GPX layer if it's the only one.
     
    168167        if(checkForErrors(username, password, description, gpxData))
    169168            return;
    170        
     169
    171170        // Clean description/tags from disallowed chars
    172171        description = description.replaceAll("[&?/\\\\]"," ");
    173172        tags = tags.replaceAll("[&?/\\\\.,;]"," ");
    174        
     173
    175174        // Set progress dialog to indeterminate while connecting
    176175        Main.pleaseWaitDlg.progress.setValue(0);
    177         Main.pleaseWaitDlg.setIndeterminate(true); 
     176        Main.pleaseWaitDlg.setIndeterminate(true);
    178177        Main.pleaseWaitDlg.currentAction.setText(tr("Connecting..."));
    179178
    180179        try {
    181180            // Generate data for upload
    182             ByteArrayOutputStream baos  = new ByteArrayOutputStream();           
     181            ByteArrayOutputStream baos  = new ByteArrayOutputStream();
    183182            writeGpxFile(baos, "file", gpxData);
    184183            writeField(baos, "description", description);
     
    186185            writeField(baos, "public", isPublic ? "1" : "0");
    187186            writeString(baos, "--" + BOUNDARY + "--" + LINE_END);
    188            
    189             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());           
     187
     188            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    190189            HttpURLConnection conn = setupConnection(baos.size());
    191            
     190
    192191            Main.pleaseWaitDlg.progress.setMaximum(baos.size());
    193             Main.pleaseWaitDlg.setIndeterminate(false); 
    194            
     192            Main.pleaseWaitDlg.setIndeterminate(false);
     193
    195194            try {
    196195                flushToServer(bais, conn.getOutputStream());
    197196            } catch(Exception e) {}
    198            
     197
    199198            if(cancelled) {
    200199                conn.disconnect();
     
    203202                cancelled = false;
    204203            } else {
    205                 boolean success = finishUpConnection(conn);           
     204                boolean success = finishUpConnection(conn);
    206205                buttons.get(0).setEnabled(!success);
    207206                if(success)
     
    213212        }
    214213    }
    215    
     214
    216215    /**
    217216     * This function sets up the upload URL and logs in using the username and password given
     
    225224        String auth = username + ":" + password;
    226225        ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth));
    227        
     226
    228227        // Upload URL
    229228        URL url = new URL("http://www.openstreetmap.org/api/" + API_VERSION + "/gpx/create");
    230        
     229
    231230        // Set up connection and log in
    232231        HttpURLConnection c = (HttpURLConnection) url.openConnection();
     
    235234        c.setRequestMethod("POST");
    236235        c.setDoOutput(true);
    237         c.addRequestProperty("Authorization", "Basic " + Base64.encode(bytes));           
     236        c.addRequestProperty("Authorization", "Basic " + Base64.encode(bytes));
    238237        c.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
    239238        c.addRequestProperty("Connection", "close"); // counterpart of keep-alive
    240239        c.addRequestProperty("Expect", "");
    241240        c.connect();
    242        
     241
    243242        return c;
    244243    }
    245    
     244
    246245    /**
    247246     * This function checks if the given connection finished up fine, closes it and returns the result.
    248247     * It also posts the result (or errors) to OutputDisplay.
    249      
     248
    250249     * @param HttpURLConnection The connection to check/finish up
    251250     */
     
    253252        String returnMsg = c.getResponseMessage();
    254253        boolean success = returnMsg.equals("OK");
    255        
     254
    256255        if (c.getResponseCode() != 200) {
    257256            if (c.getHeaderField("Error") != null)
    258257                returnMsg += "\n" + c.getHeaderField("Error");
    259258        }
    260        
     259
    261260        OutputDisplay.setText(success
    262261            ? tr("GPX upload was successful")
     
    266265        return success;
    267266    }
    268    
     267
    269268    /**
    270269     * Uploads a given InputStream to a given OutputStream and sets progress
     
    274273    private void flushToServer(InputStream in, OutputStream out) throws Exception {
    275274        // Upload in 10 kB chunks
    276                 byte[] buffer = new byte[10000];
    277                 int nread;
    278                 int cur = 0;
    279                 synchronized (in) {
    280                         while ((nread = in.read(buffer, 0, buffer.length)) >= 0) {
    281                                 out.write(buffer, 0, nread);
    282                                 cur += nread;
     275        byte[] buffer = new byte[10000];
     276        int nread;
     277        int cur = 0;
     278        synchronized (in) {
     279            while ((nread = in.read(buffer, 0, buffer.length)) >= 0) {
     280                out.write(buffer, 0, nread);
     281                cur += nread;
    283282                out.flush();
    284283                Main.pleaseWaitDlg.progress.setValue(cur);
    285284                Main.pleaseWaitDlg.currentAction.setText(getProgressText(cur));
    286                
     285
    287286                if(cancelled)
    288287                    break;
    289                         }
    290                 }
    291                 if(!cancelled)
     288            }
     289        }
     290        if(!cancelled)
    292291            out.flush();
    293292        Main.pleaseWaitDlg.currentAction.setText("Waiting for server reply...");
    294                 buffer = null;
    295         }
    296    
     293        buffer = null;
     294    }
     295
    297296    /**
    298297     * Generates the output string displayed in the PleaseWaitDialog.
     
    306305                        percent, formatBytes(cur), formatBytes(max));
    307306    }
    308    
     307
    309308    /**
    310309     * Nicely calculates given bytes into MB, kB and B (with units)
     
    351350
    352351    /**
    353      * Checks if a GPX layer is selected and returns the result. Also writes an error 
     352     * Checks if a GPX layer is selected and returns the result. Also writes an error
    354353     * message to OutputDisplay if result is false.
    355354     * @return boolean True, if /no/ GPX layer is selected
     
    366365    }
    367366
    368    
     367
    369368    /**
    370369     * This creates the uploadTask that does the actual work and hands it to the main.worker to be executed.
     
    372371    private void setupUpload() {
    373372        if(checkForGPXLayer()) return;
    374        
     373
    375374        // Disable Upload button so users can't just upload that track again
    376375        buttons.get(0).setEnabled(false);
     
    389388            }
    390389        };
    391        
     390
    392391        Main.worker.execute(uploadTask);
    393392    }
    394    
     393
    395394    /**
    396395     * Writes textfields (like in webbrowser) to the given ByteArrayOutputStream
     
    425424        writeLineEnd(baos);
    426425    }
    427    
     426
    428427    /**
    429428     * Writes a String to the given ByteArrayOutputStream
     
    436435        } catch(Exception e) {}
    437436    }
    438    
     437
    439438    /**
    440439     * Writes a newline to the given ByteArrayOutputStream
     
    444443        writeString(baos, LINE_END);
    445444    }
    446    
     445
    447446    /**
    448447     * Writes a boundary line to the given ByteArrayOutputStream
     
    453452        writeLineEnd(baos);
    454453    }
    455    
     454
    456455    /**
    457456     * Returns the filename of the GPX file to be upload. If not available, returns current date
     
    462461       return filename.equals("") ? datename : filename;
    463462    }
    464    
    465     /**
    466      * Defines a default size for the dialog
    467      */
    468     @Override protected Dimension findMaxDialogSize() {
    469         setResizable(false);
    470         return new Dimension(300, 230);
    471     }
    472    
     463
    473464    /**
    474465     * Overrides the default actions. Will not close the window when upload trace is clicked
Note: See TracChangeset for help on using the changeset viewer.