⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 entryeditor.java

📁 基于Eclipse RCP开发的管理工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                {
                    viewer.refresh();
                }
                else
                {
                    viewer.refresh(event.getTargetObject());
                }

                // Since the model has changed, the properties view, if open
                // needs updating as well.
                // Reselect the current viewer selection to cause a properties
                // refresh
                // TODO: is this a hack or accepted practice?
                // purist point of view: should a part know about other views?
                // (see:
                // http://dev.eclipse.org/newslists/news.eclipse.tools.gef/msg12694.html)
                viewer.setSelection(viewer.getSelection());

                break;
            }
            case REMOVE:
            {
                viewer.remove(event.getTargetObject());
                break;
            }
            case REMOVE_ALL:
            {
                viewer.refresh();
                break;
            }
            default:
            {
                String msg = "Unhandled event type: " + event.getType();
                throw new IllegalArgumentException(msg);
            }
        }

    }

    private void setDirty(boolean dirty)
    {
        if (this.dirty != dirty)
        {
            this.dirty = dirty;
            // this method causes the editor to become aware of the dirty
            // change, and will update the editor tab to prefix the name
            // with a '*' to indicate to the user that the resource
            // backing the editor has been changed.
            firePropertyChange(IEditorPart.PROP_DIRTY);
        }
    }

    /**
     * This method marks the newly created item by selecting the inserted item.
     * Will also set the view selection to trigger a properties view update.
     * 
     * @param newEntry
     */
    private void selectNewItem(ManagerItem newEntry, Table table)
    {
        for (int i = 0; i < table.getItemCount(); i++)
        {
            ManagerItem item = (ManagerItem) table.getItem(i).getData();
            if (item.getName() == newEntry.getName())
            {
                table.deselectAll();
                table.select(i);

                System.out.println("notifying listeners for selection of: "
                        + item.getName());
                notifyListenersSelectionEvent(item, table);
            }
        }
    }

    /**
     * Helper method to notify selection listeners for the table (such as the
     * properties view) when a new item is selected) When the selection changes
     * programmatically.
     * 
     * (see SWT FAQ: Why are some events like Selection not fired in response to
     * programmatic widget changes?)
     * 
     * @param item
     *            item selected
     */
    private void notifyListenersSelectionEvent(ManagerItem item, Widget widget)
    {
        Event event = new Event();
        event.type = SWT.Selection;
        event.data = item;
        widget.notifyListeners(SWT.Selection, event);
    }

    public EntryEditorInput getEntryEditorInput()
    {
        return editorInput;
    }

    public void setStatusField(String category,
            StatusLineContribution statusField)
    {
        if (statusField != null)
        {
            if (statusFields == null)
            {
                statusFields = new HashMap<String, StatusLineContribution>();
            }
            statusFields.put(category, statusField);
            // updateStatusField(category);

        }
        else if (statusFields != null)
        {
            statusFields.remove(category);
        }
    }

    protected StatusLineContribution getStatusField(String category)
    {
        if (category != null && statusFields != null)
        {
            return statusFields.get(category);
        }
        else
        {
            return null;
        }
    }

    private void updateTablePositionField()
    {
        if (viewer != null)
        {
            Table table = viewer.getTable();
            int selIndex = table.getSelectionIndex() + 1;
            int cnt = table.getItemCount();
            StatusLineContribution field = getStatusField(INPUT_POSITION);
            if (field != null)
            {
                field.setText("" + selIndex + " : " + cnt);
            }
        }
    }

    @Override
    protected void setInput(IEditorInput input)
    {
        super.setInput(input);
    }

    private class ActivationListener implements IPartListener
    {

        /** Cache of the active workbench part. */
        private IWorkbenchPart activePart;

        /** Indicates whether activation handling is currently be done. */
        private boolean isHandlingActivation = false;

        public void partActivated(IWorkbenchPart part)
        {
            activePart = part;
            handleActivation();
        }

        private void handleActivation()
        {
            if (isHandlingActivation)
            {
                return;
            }

            if (activePart == EntryEditor.this)
            {
                isHandlingActivation = true;
                try
                {
                    updateStatusFields();
                }
                finally
                {
                    isHandlingActivation = false;
                }
            }
        }

        private void updateStatusFields()
        {
            StatusLineContribution modeField = getStatusField(INPUT_MODE);
            if (modeField != null)
            {
                // TODO: implement
                modeField.setText("Writable");
            }

            updateTablePositionField();
        }

        public void partBroughtToTop(IWorkbenchPart part)
        {
        }

        public void partClosed(IWorkbenchPart part)
        {
            // since part is closed, no need to keep observers around
            if (part == EntryEditor.this)
            {
                managerItems.clearObservers();
            }
        }

        public void partDeactivated(IWorkbenchPart part)
        {
        }

        public void partOpened(IWorkbenchPart part)
        {
        }

    }

    public void exportEntries(File destination)
    {
        Storage.exportEntries(managerItems, destination);
    }

    /**
     * Creates a new password entry object and adds it to the container managed
     * by the editor. A generic unique name and a default password is generated
     * for the new entry.
     */
    public void addNewPasswordEntry()
    {
        // TODO: remove passwordentry/label code duplication if possible

        if (useDialogForCreate())
        {
            PasswordEntry newEntry = new PasswordEntry("", "");

            newEntry.setPassword(PasswordGenerator.DEFAULT.generate(16));

            DuplicateChecker dupCheck = new DuplicateChecker()
            {
                public boolean exists(String name)
                {
                    return managerItems.containsPasswordEntry(name);
                }
            };

            // todo: check prefs to see whether dialog should be used
            EditPasswordDialog dlg = new EditPasswordDialog(getSite()
                    .getShell(), dupCheck, DialogType.CREATE);
            dlg.setEntry(newEntry);
            if (dlg.open() == Window.OK)
            {
                newEntry = dlg.getEntry();

                managerItems.add(newEntry);
                selectNewItem(newEntry, viewer.getTable());
            }
        }
        else
        {
            PasswordEntry newEntry = newPasswordEntry();
            managerItems.add(newEntry);
            selectNewItem(newEntry, viewer.getTable());
        }
    }

    private PasswordEntry newPasswordEntry()
    {
        String newName = "New password entry " + newEntryCnt++;
        while (managerItems.containsPasswordEntry(newName))
        {
            newName = "New password entry " + newEntryCnt++;
        }

        PasswordEntry newEntry = new PasswordEntry(newName, "");

        // generate default password
        // TODO: make configurable
        newEntry.setPassword(PasswordGenerator.DEFAULT.generate(16));
        return newEntry;
    }

    // TODO: move to prefs util class
    public boolean useDialogForCreate()
    {
        IPreferencesService prefs = Platform.getPreferencesService();
        boolean useDialog = prefs.getBoolean(Application.PLUGIN_ID,
                GeneralPreferencePage.USE_DIALOG_FOR_CREATE, false, null);

        return useDialog;
    }

    public void addNewLabel()
    {
        if (useDialogForCreate())
        {
            ItemLabel newLabel = new ItemLabel("");

            DuplicateChecker dupCheck = new DuplicateChecker()
            {
                public boolean exists(String name)
                {
                    return managerItems.containsLabel(name);
                }
            };

            EditLabelDialog dlg = new EditLabelDialog(getSite().getShell(),
                    dupCheck, DialogType.CREATE);
            dlg.setEntry(newLabel);
            if (dlg.open() == Window.OK)
            {
                newLabel = dlg.getEntry();

                managerItems.add(newLabel);
                selectNewItem(newLabel, viewer.getTable());
            }
        }
        else
        // non interactive
        {
            ItemLabel newLabel = newLabel();
            managerItems.add(newLabel);
            selectNewItem(newLabel, viewer.getTable());
        }

    }

    private ItemLabel newLabel()
    {
        String newName = "New label " + newLabelCnt++;
        while (managerItems.containsPasswordEntry(newName))
        {
            newName = "New label " + newLabelCnt++;
        }
        ItemLabel label = new ItemLabel(newName);
        return label;
    }

    public void merge(File inFile)
    {
        GetPasswordDialog passwordDialog = new GetPasswordDialog(getSite()
                .getShell(), editorInput.getFile());
        int ret = passwordDialog.open();
        if (ret == Window.CANCEL)
        {
            // cancel
            return;
        }

        String password = passwordDialog.getValue();

        ItemContainer inEntries = Storage.load(inFile, password);

        MergeDialog dlg = new MergeDialog(getSite().getShell(), "",
                managerItems, inEntries);
        ret = dlg.open();
        if (ret == Window.OK)
        {
            // replace entries with accepted merge list
            managerItems.clear();
            managerItems.addAll(dlg.getMergedEntries());
        }
    }

    public void setTableFilter(String text)
    {
        viewerFilter.setFilter(text);
        viewer.refresh();
    }

    /**
     * Used by LabelsView to filter the item list on the selected label. If the
     * label is null, the label filter is cleared.
     * 
     * @param label
     *            ItemLabel to filter on
     */
    public void filterOnLabel(ItemLabel label)
    {
        viewerFilter.setFilter(label);
        viewer.refresh();
    }

    /**
     * Since the editor controls the labels, the view obtains them from here.
     * 
     * @return the ItemLabels managed by this editor.
     */
    public List<ItemLabel> getLabels()
    {
        return managerItems.getLabels();
    }

    public void addItemObserver(Observer observer)
    {
        managerItems.addObserver(observer);
    }

    public void removeItemObserver(Observer observer)
    {
        managerItems.removeObserver(observer);
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -