Example usage for javax.swing JInternalFrame getClientProperty

List of usage examples for javax.swing JInternalFrame getClientProperty

Introduction

In this page you can find the example usage for javax.swing JInternalFrame getClientProperty.

Prototype

public final Object getClientProperty(Object key) 

Source Link

Document

Returns the value of the property with the specified key.

Usage

From source file:GUIUtils.java

/**
 * Return <TT>true</TT> if <TT>frame</TT> is a tool window. I.E. is the
 * <TT>JInternalFrame.isPalette</TT> set to <TT>Boolean.TRUE</TT>?
 * //from  w ww .ja v  a 2s  .com
 * @param frame
 *          The <TT>JInternalFrame</TT> to be checked.
 * 
 * @throws IllegalArgumentException
 *           If <TT>frame</TT> is <TT>null</TT>.
 */
public static boolean isToolWindow(JInternalFrame frame) {
    if (frame == null) {
        throw new IllegalArgumentException("null JInternalFrame passed");
    }

    final Object obj = frame.getClientProperty("JInternalFrame.isPalette");
    return obj != null && obj == Boolean.TRUE;
}

From source file:org.docx4all.ui.main.WordMLEditor.java

private int showConfirmClosingInternalFrame(JInternalFrame iframe, String resourceKeyPrefix) {
    int answer = JOptionPane.CANCEL_OPTION;

    String filePath = (String) iframe.getClientProperty(WordMLDocument.FILE_PATH_PROPERTY);

    ResourceMap rm = getContext().getResourceMap();
    String title = rm.getString(resourceKeyPrefix + ".dialog.title") + " "
            + filePath.substring(filePath.lastIndexOf(File.separator) + 1);
    String message = filePath + "\n" + rm.getString(resourceKeyPrefix + ".confirmMessage");
    Object[] options = { rm.getString(resourceKeyPrefix + ".confirm.saveNow"),
            rm.getString(resourceKeyPrefix + ".confirm.dontSave"),
            rm.getString(resourceKeyPrefix + ".confirm.cancel") };
    answer = showConfirmDialog(title, message, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
            options, options[0]);//from   w w w . ja v  a2 s .c  o  m
    if (answer == JOptionPane.CANCEL_OPTION) {
        ;
    } else if (answer == JOptionPane.YES_OPTION) {
        boolean success = FileMenu.getInstance().save(iframe, null, FileMenu.SAVE_FILE_ACTION_NAME);
        if (success) {
            getToolbarStates().setDocumentDirty(iframe, false);
            getToolbarStates().setLocalEditsEnabled(iframe, false);
        } else {
            answer = JOptionPane.CANCEL_OPTION;
        }
    } else {
        //getToolbarStates().setDocumentDirty(iframe, false);
    }

    return answer;
}

From source file:org.docx4all.ui.menu.FileMenu.java

private RETURN_TYPE saveAsFile(String callerActionName, ActionEvent actionEvent, String fileType) {
    Preferences prefs = Preferences.userNodeForPackage(getClass());
    WordMLEditor editor = WordMLEditor.getInstance(WordMLEditor.class);
    ResourceMap rm = editor.getContext().getResourceMap(getClass());

    JInternalFrame iframe = editor.getCurrentInternalFrame();
    String oldFilePath = (String) iframe.getClientProperty(WordMLDocument.FILE_PATH_PROPERTY);

    VFSJFileChooser chooser = createFileChooser(rm, callerActionName, iframe, fileType);

    RETURN_TYPE returnVal = chooser.showSaveDialog((Component) actionEvent.getSource());
    if (returnVal == RETURN_TYPE.APPROVE) {
        FileObject selectedFile = getSelectedFile(chooser, fileType);

        boolean error = false;
        boolean newlyCreatedFile = false;

        if (selectedFile == null) {

            // Should never happen, whether the file exists or not

        } else {/*ww w  . j a v  a  2  s  .  co m*/

            //Check selectedFile's existence and ask user confirmation when needed.
            try {
                boolean selectedFileExists = selectedFile.exists();
                if (!selectedFileExists) {
                    FileObject parent = selectedFile.getParent();
                    String uri = UriParser.decode(parent.getName().getURI());

                    if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") > -1
                            && parent.getName().getScheme().startsWith("file") && !parent.isWriteable()
                            && (uri.indexOf("/Documents") > -1 || uri.indexOf("/My Documents") > -1)) {
                        //TODO: Check whether we still need this workaround.
                        //See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4939819
                        //Re: File.canWrite() returns false for the "My Documents" directory (win)
                        String localpath = org.docx4j.utils.VFSUtils.getLocalFilePath(parent);
                        File f = new File(localpath);
                        f.setWritable(true, true);
                    }

                    selectedFile.createFile();
                    newlyCreatedFile = true;

                } else if (!selectedFile.getName().getURI().equalsIgnoreCase(oldFilePath)) {
                    String title = rm.getString(callerActionName + ".Action.text");
                    String message = VFSUtils.getFriendlyName(selectedFile.getName().getURI()) + "\n"
                            + rm.getString(callerActionName + ".Action.confirmMessage");
                    int answer = editor.showConfirmDialog(title, message, JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE);
                    if (answer != JOptionPane.YES_OPTION) {
                        selectedFile = null;
                    }
                } // if (!selectedFileExists)

            } catch (FileSystemException exc) {
                exc.printStackTrace();//ignore
                log.error("Couldn't create new file or assure file existence. File = "
                        + selectedFile.getName().getURI());
                selectedFile = null;
                error = true;
            }
        }

        //Check whether there has been an error, cancellation by user
        //or may proceed to saving file.
        if (selectedFile != null) {
            //Proceed to saving file
            String selectedPath = selectedFile.getName().getURI();
            if (log.isDebugEnabled()) {
                log.debug("saveAsFile(): selectedFile = " + VFSUtils.getFriendlyName(selectedPath));
            }

            prefs.put(Constants.LAST_OPENED_FILE, selectedPath);
            if (selectedFile.getName().getScheme().equals("file")) {
                prefs.put(Constants.LAST_OPENED_LOCAL_FILE, selectedPath);
            }
            PreferenceUtil.flush(prefs);

            boolean success = false;
            if (EXPORT_AS_NON_SHARED_DOC_ACTION_NAME.equals(callerActionName)) {
                log.info("saveAsFile(): Exporting as non shared document to "
                        + VFSUtils.getFriendlyName(selectedPath));
                success = export(iframe, selectedPath, callerActionName);
                if (success) {
                    prefs.put(Constants.LAST_OPENED_FILE, selectedPath);
                    if (selectedPath.startsWith("file:")) {
                        prefs.put(Constants.LAST_OPENED_LOCAL_FILE, selectedPath);
                    }
                    PreferenceUtil.flush(prefs);
                    log.info("saveAsFile(): Opening " + VFSUtils.getFriendlyName(selectedPath));
                    editor.createInternalFrame(selectedFile);
                }
            } else {
                success = save(iframe, selectedPath, callerActionName);
                if (success) {
                    if (Constants.DOCX_STRING.equals(fileType) || Constants.FLAT_OPC_STRING.equals(fileType)) {
                        //If saving as .docx then update the document dirty flag 
                        //of toolbar states as well as internal frame title.
                        editor.getToolbarStates().setDocumentDirty(iframe, false);
                        editor.getToolbarStates().setLocalEditsEnabled(iframe, false);
                        FileObject file = null;
                        try {
                            file = VFSUtils.getFileSystemManager().resolveFile(oldFilePath);
                            editor.updateInternalFrame(file, selectedFile);
                        } catch (FileSystemException exc) {
                            ;//ignore
                        }
                    } else {
                        //Because document dirty flag is not cleared
                        //and internal frame title is not changed,
                        //we present a success message.
                        String title = rm.getString(callerActionName + ".Action.text");
                        String message = VFSUtils.getFriendlyName(selectedPath) + "\n"
                                + rm.getString(callerActionName + ".Action.successMessage");
                        editor.showMessageDialog(title, message, JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            }

            if (!success && newlyCreatedFile) {
                try {
                    selectedFile.delete();
                } catch (FileSystemException exc) {
                    log.error("saveAsFile(): Saving failure and cannot remove the newly created file = "
                            + selectedPath);
                    exc.printStackTrace();
                }
            }
        } else if (error) {
            log.error("saveAsFile(): selectedFile = NULL");
            String title = rm.getString(callerActionName + ".Action.text");
            String message = rm.getString(callerActionName + ".Action.errorMessage");
            editor.showMessageDialog(title, message, JOptionPane.ERROR_MESSAGE);
        }

    } //if (returnVal == JFileChooser.APPROVE_OPTION)

    return returnVal;
}

From source file:org.docx4all.ui.menu.FileMenu.java

private VFSJFileChooser createFileChooser(ResourceMap resourceMap, String callerActionName,
        JInternalFrame iframe, String filteredFileExtension) {

    String filePath = null;/*from   w  w w .  j a v a 2 s .com*/
    if (EXPORT_AS_SHARED_DOC_ACTION_NAME.equals(callerActionName)) {
        Preferences prefs = Preferences.userNodeForPackage(getClass());
        filePath = prefs.get(Constants.LAST_OPENED_FILE, Constants.EMPTY_STRING);
    } else {
        filePath = (String) iframe.getClientProperty(WordMLDocument.FILE_PATH_PROPERTY);
    }

    FileObject file = null;
    FileObject dir = null;
    try {
        file = VFSUtils.getFileSystemManager().resolveFile(filePath);
        dir = file.getParent();
    } catch (FileSystemException exc) {
        ;//ignore
    }

    return createFileChooser(resourceMap, dir, filteredFileExtension);
}

From source file:org.docx4all.ui.menu.FileMenu.java

/**
 * Saves editor documents to a file./*from   w w  w. j a  v  a  2s.c  o  m*/
 * 
 * Internal frame may have two editors for presenting two different views
 * to user namely editor view and source view. WordMLTextPane is used for 
 * editor view and JEditorPane for source view.
 * The contents of these two editors are synchronized when user switches
 * from one view to the other. Therefore, there will be ONLY ONE editor 
 * that is dirty and has to be saved by this method.
 * 
 * @param iframe
 * @param saveAsFilePath
 * @param callerActionName
 * @return
 */
public boolean save(JInternalFrame iframe, String saveAsFilePath, String callerActionName) {
    boolean success = true;

    if (saveAsFilePath == null) {
        saveAsFilePath = (String) iframe.getClientProperty(WordMLDocument.FILE_PATH_PROPERTY);
    }

    if (log.isDebugEnabled()) {
        log.debug("save(): filePath=" + VFSUtils.getFriendlyName(saveAsFilePath));
    }

    WordMLTextPane editorView = SwingUtil.getWordMLTextPane(iframe);
    JEditorPane sourceView = SwingUtil.getSourceEditor(iframe);

    if (sourceView != null && !((Boolean) sourceView.getClientProperty(Constants.LOCAL_VIEWS_SYNCHRONIZED_FLAG))
            .booleanValue()) {
        //signifies that Source View is not synchronised with Editor View yet.
        //Therefore, it is dirty and has to be saved.
        if (editorView != null && editorView.getWordMLEditorKit().getPlutextClient() != null) {
            //Document has to be saved from editor view 
            //by committing local edits
            success = false;
        } else {
            EditorKit kit = sourceView.getEditorKit();
            Document doc = sourceView.getDocument();
            WordprocessingMLPackage wmlPackage = (WordprocessingMLPackage) doc
                    .getProperty(WordMLDocument.WML_PACKAGE_PROPERTY);

            DocUtil.write(kit, doc, wmlPackage);
            success = save(wmlPackage, saveAsFilePath, callerActionName);

            if (success) {
                if (saveAsFilePath.endsWith(Constants.DOCX_STRING)
                        || saveAsFilePath.endsWith(Constants.FLAT_OPC_STRING)) {
                    doc.putProperty(WordMLDocument.FILE_PATH_PROPERTY, saveAsFilePath);
                    iframe.putClientProperty(WordMLDocument.FILE_PATH_PROPERTY, saveAsFilePath);
                }
            }
        }
        return success;
    }
    sourceView = null;

    if (editorView == null) {
        ;//pass

    } else if (editorView.getWordMLEditorKit().getPlutextClient() != null) {
        if (saveAsFilePath.equals(editorView.getDocument().getProperty(WordMLDocument.FILE_PATH_PROPERTY))) {
            success = commitLocalChanges(editorView, callerActionName);

        } else {
            //TODO: Enable saving Plutext document as a new file.
            WordMLEditor wmlEditor = WordMLEditor.getInstance(WordMLEditor.class);
            ResourceMap rm = wmlEditor.getContext().getResourceMap(getClass());
            String title = rm.getString(callerActionName + ".Action.text");
            StringBuilder message = new StringBuilder();
            message.append("File ");
            message.append(saveAsFilePath);
            message.append(Constants.NEWLINE);
            message.append(rm.getString(callerActionName + ".Action.wrong.fileName.infoMessage"));
            wmlEditor.showMessageDialog(title, message.toString(), JOptionPane.INFORMATION_MESSAGE);

            success = false;
        }
    } else {
        WordMLEditorKit kit = (WordMLEditorKit) editorView.getEditorKit();
        kit.saveCaretText();

        Document doc = editorView.getDocument();
        DocumentElement elem = (DocumentElement) doc.getDefaultRootElement();
        DocumentML rootML = (DocumentML) elem.getElementML();

        //Do not include the last paragraph when saving.
        //After saving we put it back.
        elem = (DocumentElement) elem.getElement(elem.getElementCount() - 1);
        ElementML paraML = elem.getElementML();
        ElementML bodyML = paraML.getParent();
        paraML.delete();

        success = save(rootML.getWordprocessingMLPackage(), saveAsFilePath, callerActionName);

        //Remember to put 'paraML' as last paragraph
        bodyML.addChild(paraML);

        if (success) {
            if (saveAsFilePath.endsWith(Constants.DOCX_STRING)) {
                doc.putProperty(WordMLDocument.FILE_PATH_PROPERTY, saveAsFilePath);
                iframe.putClientProperty(WordMLDocument.FILE_PATH_PROPERTY, saveAsFilePath);
            }
        }
    }

    return success;
}