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

📄 passwordentrypropertysource.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 java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

import com.s10r.manager.model.PasswordEntry;


class HiddenTextLabelProvider extends LabelProvider
{
	@Override
	public String getText(Object element)
	{
		String plainText = super.getText(element);
		if (plainText == null || plainText.length() == 0)
		{
			return "";
		}
		else
		{
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < super.getText(element).length(); i++)
			{
				sb.append('*');
			}
			return sb.toString();
		}
	}
}

public class PasswordEntryPropertySource extends ManagerItemPropertySource
    
{
	protected static final String PROPERTY_ID = "id";

	protected static final String PROPERTY_LAST_USED = "lastUsedDate";

	protected static final String PROPERTY_PASSWORD = "password";

	protected static final String PROPERTY_USAGE_CNT = "usageCnt";

	protected static final String PROPERTY_URL = "url";

	protected static final String PROPERTY_LABELS = "labels";

	private Object PropertiesTable[][] = null;

	public PasswordEntryPropertySource(PasswordEntry task)
	{
		super(task);
		

		PropertiesTable = new Object[][] {
				{ PROPERTY_ID,  createHiddenTextPropertyDescriptor(PROPERTY_ID, "id") },
				{ PROPERTY_URL, new TextPropertyDescriptor(PROPERTY_URL, "url") },
				{ PROPERTY_USAGE_CNT,
						new PropertyDescriptor(PROPERTY_USAGE_CNT, "usage count") },
				{
						PROPERTY_PASSWORD,
						createPasswordPropertyDescriptor(PROPERTY_PASSWORD, "password") },
						{
							PROPERTY_LABELS,
							new PropertyDescriptor(PROPERTY_LABELS, "labels") },
				{ PROPERTY_LAST_USED,
						new PropertyDescriptor(PROPERTY_LAST_USED, "last used") } };
 

	}

	private PasswordPropertyDescriptor createPasswordPropertyDescriptor(
			Object id, String displayName)
	{
		PasswordPropertyDescriptor passDesc = new PasswordPropertyDescriptor(
				id, displayName);
		// much less work than ILabelProvider
		// replace text with '*'
		passDesc.setLabelProvider(new HiddenTextLabelProvider());
		return passDesc;
	}

	
	private HiddenTextPropertyDescriptor createHiddenTextPropertyDescriptor(
			Object id, String displayName)
	{
		HiddenTextPropertyDescriptor passDesc = new HiddenTextPropertyDescriptor(
				id, displayName);
		// much less work than ILabelProvider
		// replace text with '*'
		passDesc.setLabelProvider(new HiddenTextLabelProvider());
		return passDesc;
	}


	@Override
	public IPropertyDescriptor[] getPropertyDescriptors()
	{
		List<IPropertyDescriptor> propertyDescriptors = new ArrayList<
	     	IPropertyDescriptor>();
		
		super.fillPropertyDescriptors(propertyDescriptors);
		
		for (int i = 0; i < PropertiesTable.length; i++)
		{
			// Add each property supported.

			PropertyDescriptor descriptor;

			descriptor = (PropertyDescriptor) PropertiesTable[i][1];
			
			propertyDescriptors.add(descriptor);
			
			descriptor.setCategory("Basic");
		}

		// Return it.
		return propertyDescriptors.toArray(new IPropertyDescriptor[propertyDescriptors.size()]);

	}

	@Override
	public Object getPropertyValue(Object name)
	{
		Object ret = "";
		
		if (name.equals(PROPERTY_LAST_USED))
		{
			ret = getEntry().getLastUsed();
		}
		else if (name.equals(PROPERTY_ID))
		{
			// TODO: make configurable: to show or not
			// TODO: create password editor
			// cannnot return "***" here as this will be stored in
			// the model
			ret =  getEntry().getId();
		}
		else if (name.equals(PROPERTY_PASSWORD))
		{
			ret = getEntry().getPassword();
		}
		else if (name.equals(PROPERTY_USAGE_CNT))
		{
			ret = getEntry().getUsageCnt();
		}
		else if (name.equals(PROPERTY_URL))
		{
			ret =  getEntry().getUrl();
		}
		else if (name.equals(PROPERTY_LABELS))
		{
			ret =  getEntry().getLabelsString();
		}
		else
		{
			// use default
			ret = super.getPropertyValue(name);
		}

		// property editors don't like nulls: they make the
		// field readonly
		return ret == null ? "" : ret;

	}

	@Override
	public void setPropertyValue(Object name, Object value)
	{
		if (name.equals(PROPERTY_ID))
		{
			getEntry().setId((String) value);
			getEntry().setLastUpdated();
		}
		else if (name.equals(PROPERTY_PASSWORD))
		{
			getEntry().setPassword((String) value);
			getEntry().setLastUpdated();
		}
		else if (name.equals(PROPERTY_URL))
		{
			getEntry().setUrl((String) value);
			getEntry().setLastUpdated();
		}
		else
		{
			super.setPropertyValue(name, value);
		}
	}

	private PasswordEntry getEntry()
	{
	   return (PasswordEntry)getItem();
	}
}

⌨️ 快捷键说明

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