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

📄 passwordentryview.java

📁 基于Eclipse RCP开发的管理工具
💻 JAVA
字号:
/*  
 * Copyright 2006 Marcel Schoffelmeer 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.s10r.manager.view;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

import com.s10r.manager.model.PasswordEntry;
import com.s10r.swt.StyledTextBuilder;
import com.s10r.util.DateUtil;
import com.s10r.util.Observer;
import com.s10r.util.UpdateEvent;

/*
 * TODO: show hyperlink/launch browser on click for URL
 * 
 */

/**
 * PasswordEntryView is a KeePass style view that shows the password entry
 * fields in an SWT StyledText field, bolding the field names. This view listens
 * for selection changes and keeps track of the password entry selected. This
 * view also registers itself as a model observer for the select password entry,
 * for password entry changes to be reflected immediately in this view.
 * 
 * @author mms
 * 
 */
public class PasswordEntryView extends ViewPart implements ISelectionListener,
		Observer
{
	public static final String ID = "rcpmanagerpasswordentryview";

	private StyledText styledText;

	private PasswordEntry currentPasswordEntry = null;

	public PasswordEntryView()
	{
		super();

	}

	@Override
	public void createPartControl(Composite parent)
	{
		styledText = new StyledText(parent, SWT.BORDER);
		styledText.setText("");
		styledText.setWordWrap(true);
		styledText.setEditable(false);
		styledText.setVisible(true);

		getSite().getPage().addSelectionListener(this);
	}

	/**
	 * ISelectionListener callback when a new password entry is selected.
	 * 
	 */
	public void selectionChanged(IWorkbenchPart part, ISelection selection)
	{
		if (selection != null && selection instanceof IStructuredSelection)
		{
			Object firstElement = ((IStructuredSelection) selection)
					.getFirstElement();

			if (firstElement == null)
			{
				// do nothing, not sure why there are selection callbacks
				// if there's no element selected.. (de-select event?)
			}
			else if (firstElement instanceof PasswordEntry)
			{
				PasswordEntry entry = (PasswordEntry) firstElement;

				// If the selection changed, remove this view as a listener
				// to changes from the current entry, and add it to the new one
				if (entry != currentPasswordEntry)
				{
					if (currentPasswordEntry != null)
					{
						currentPasswordEntry.removeObserver(this);
					}

					// hold on to the entry so we can remove ourselves later
					currentPasswordEntry = entry;

					currentPasswordEntry.addObserver(this);

					updateText(entry);
				}
			}
			else
			// object doesn't apply to this view
			{
				styledText.setText("");
			}
		}
	}

	private void updateText(PasswordEntry entry)
	{
		if (entry == null)
		{
			throw new NullPointerException();
		}

		StyledTextBuilder stb = new StyledTextBuilder(styledText);

		stb.addNvPair("Name", entry.getName());

		stb.addPasswordNvPair("Id", entry.getId());
		stb.addPasswordNvPair("Password", entry.getPassword());

		stb.addNvPair("Description", entry.getDescription());
		stb.addNvPair("Created", DateUtil.format(entry.getCreated()));
		stb.addNvPair("Usage Count", "" + entry.getUsageCnt());
		stb.addNvPair("Last Used", DateUtil.format(entry.getLastUsed()));
		stb.addNvPair("Last Updated", DateUtil.format(entry.getLastUpdated()));
		stb.addNvPair("Last Updated On Site", DateUtil.format(entry
				.getLastUpdatedOnSite()));
		stb.addNvPair("Password Age", "" + entry.getPasswordAge() + " days");

		stb.addLinkNvPair("URL", entry.getUrl());

		stb.updateText();
	}

	public void update(UpdateEvent event)
	{

		if (event.getType() == UpdateEvent.Type.EDIT)
		{
			updateText(currentPasswordEntry);
		}
	}

	@Override
	public void dispose()
	{
		if (currentPasswordEntry != null)
		{
			currentPasswordEntry.removeObserver(this);
			currentPasswordEntry = null;
		}

		getSite().getPage().removeSelectionListener(this);

		super.dispose();

	}

	@Override
	public void setFocus()
	{

	}

}

⌨️ 快捷键说明

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