Search in sources :

Example 1 with CachedURLObject

use of net.sourceforge.processdash.net.cache.CachedURLObject in project processdash by dtuma.

the class TaskScheduleDialog method importNewSharedSchedule.

private String importNewSharedSchedule() {
    String urlStr = "http://";
    String passwordStr = "";
    String errorMessage = null;
    URL u = null;
    while (true) {
        // ask the user for the relevant information to locate the
        // schedule.
        JTextField url = new JTextField(urlStr, 40);
        url.addFocusListener(new FocusHighlighter(url));
        JTextField password = new JPasswordField(passwordStr, 10);
        password.addFocusListener(new FocusHighlighter(password));
        String urlLabel = resources.getString("Import_Schedule.URL_Label");
        String passwordLabel = resources.getString("Import_Schedule.Password_Label");
        Object message = new Object[] { errorMessage, resources.getString("Import_Schedule.Instructions"), newHBox(new JLabel("  " + urlLabel + " "), url), newHBox(new JLabel("  " + passwordLabel + " "), password) };
        if (JOptionPane.showConfirmDialog(frame, message, resources.getString("Import_Schedule.Dialog_Title"), JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)
            // if the user didn't hit the OK button, return null.
            return null;
        urlStr = url.getText();
        passwordStr = password.getText();
        if (urlStr == null || urlStr.trim().length() == 0) {
            errorMessage = resources.getString("Import_Schedule.URL_Missing");
            continue;
        }
        if (urlStr.indexOf("/ev+/") != -1) {
            errorMessage = resources.getString("Import_Schedule.Pub_URL");
            continue;
        }
        try {
            u = new URL(urlStr.trim() + XML_QUERY_SUFFIX);
        } catch (MalformedURLException mue) {
            errorMessage = resources.getString("Import_Schedule.URL_Invalid");
            continue;
        }
        break;
    }
    // fetch the specified schedule.
    if (passwordStr != null) {
        passwordStr = passwordStr.trim();
        if (passwordStr.length() == 0)
            passwordStr = null;
    }
    CachedObject importedSchedule = new CachedURLObject(dash.getCache(), EVTaskListCached.CACHED_OBJECT_TYPE, u, passwordStr == null ? null : "EV", passwordStr);
    // check to see if there was an error in fetching the schedule.
    errorMessage = importedSchedule.getErrorMessage();
    String remoteName = null;
    if (errorMessage == null) {
        // if we were able to successfully fetch the schedule, try
        // to interpret its contents as an XML schedule.
        remoteName = EVTaskListCached.getNameFromXML(importedSchedule.getString("UTF-8"));
        // record an error message.
        if (remoteName == null)
            errorMessage = resources.getString("Import_Schedule.Invalid_Schedule");
    }
    // if there was any error, ask the user if they want to continue.
    if (errorMessage != null) {
        errorMessage = CachedURLObject.translateMessage(resources, "Import_Schedule.", errorMessage);
        Object message = new Object[] { resources.getString("Import_Schedule.Error.Header"), "    " + errorMessage, resources.getString("Import_Schedule.Error.Footer") };
        if (JOptionPane.showConfirmDialog(frame, message, resources.getString("Import_Schedule.Error.Title"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE) != JOptionPane.YES_OPTION)
            return null;
    }
    // get a local name to use for this schedule.
    String localName = remoteName;
    String owner = (String) importedSchedule.getLocalAttr(CachedURLObject.OWNER_ATTR);
    if (owner != null)
        localName = resources.format("Import_Schedule.Default_Name_FMT", localName, owner);
    do {
        localName = (String) JOptionPane.showInputDialog(frame, resources.getStrings("Import_Schedule.Name_Dialog.Prompt"), resources.getString("Import_Schedule.Name_Dialog.Title"), JOptionPane.PLAIN_MESSAGE, null, null, localName);
        if (localName != null)
            localName = localName.replace('/', ' ').trim();
    } while (localName == null || localName.length() == 0);
    importedSchedule.setLocalAttr(EVTaskListCached.LOCAL_NAME_ATTR, localName);
    // return the name of the cached schedule object
    return EVTaskListCached.buildTaskListName(importedSchedule.getID(), localName);
}
Also used : MalformedURLException(java.net.MalformedURLException) CachedURLObject(net.sourceforge.processdash.net.cache.CachedURLObject) CachedObject(net.sourceforge.processdash.net.cache.CachedObject) JPasswordField(javax.swing.JPasswordField) JLabel(javax.swing.JLabel) CachedObject(net.sourceforge.processdash.net.cache.CachedObject) EventObject(java.util.EventObject) CachedURLObject(net.sourceforge.processdash.net.cache.CachedURLObject) JTextField(javax.swing.JTextField) URL(java.net.URL)

Aggregations

MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 EventObject (java.util.EventObject)1 JLabel (javax.swing.JLabel)1 JPasswordField (javax.swing.JPasswordField)1 JTextField (javax.swing.JTextField)1 CachedObject (net.sourceforge.processdash.net.cache.CachedObject)1 CachedURLObject (net.sourceforge.processdash.net.cache.CachedURLObject)1