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

📄 swtskin.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * Created on May 29, 2006 4:01:04 PM
 * Copyright (C) 2006 Aelitis, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * AELITIS, SAS au capital de 46,603.30 euros
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
 */
package com.aelitis.azureus.ui.swt.skin;

import java.util.*;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.*;

import org.gudy.azureus2.core3.util.AEMonitor;
import org.gudy.azureus2.core3.util.Constants;
import org.gudy.azureus2.core3.util.Debug;

import com.aelitis.azureus.ui.skin.SkinProperties;
import com.aelitis.azureus.ui.swt.utils.ImageLoader;
import com.aelitis.azureus.ui.swt.utils.ImageLoaderFactory;

/**
 * @author TuxPaper
 * @created May 29, 2006
 *
 */
public class SWTSkin
{
	static boolean DEBUGLAYOUT = false; //System.getProperty("debuglayout") != null;

	private Map mapImageLoaders = new HashMap();

	private SWTSkinProperties skinProperties;

	private Listener handCursorListener;

	private Listener ontopPaintListener;

	// Key = Skin Object ID; Value = Control
	private HashMap mapIDsToControls = new HashMap();

	// Key = TabSet ID; Value = List of Control
	private HashMap mapTabSetToControls = new HashMap();

	// Key = Widget ID; Value = Control
	private HashMap mapPublicViewIDsToControls = new HashMap();

	private HashMap mapPublicViewIDsToListeners = new HashMap();

	private AEMonitor mapPublicViewIDsToListeners_mon = new AEMonitor(
			"SWTSkin::mapPublicViewIDsToListeners");

	private ArrayList ontopImages = new ArrayList();

	private Shell shell;

	private boolean bLayoutComplete = false;

	/**
	 * 
	 */
	public SWTSkin() {
		skinProperties = new SWTSkinPropertiesImpl();
		ImageLoaderFactory.createInstance(Display.getDefault(), skinProperties);

		ontopPaintListener = new Listener() {
			public void handleEvent(Event event) {
				for (Iterator iter = ontopImages.iterator(); iter.hasNext();) {
					SWTSkinObject skinObject = (SWTSkinObject) iter.next();

					Control control = skinObject.getControl();
					if (control == null) {
						continue;
					}
					Rectangle bounds = control.getBounds();
					Point point = control.toDisplay(0, 0);
					bounds.x = point.x;
					bounds.y = point.y;

					Rectangle eventBounds = event.getBounds();
					point = ((Control) event.widget).toDisplay(0, 0);
					eventBounds.x += point.x;
					eventBounds.y += point.y;

					//System.out.println(eventBounds + ";" + bounds);

					if (eventBounds.intersects(bounds)) {
						Point dst = new Point(bounds.x - point.x, bounds.y - point.y);

						//System.out.println("Painting on " + event.widget + " at " + dst);
						Image image = (Image) control.getData("image");
						// TODO: Clipping otherwise alpha will multiply
						//event.gc.setClipping(eventBounds);
						event.gc.drawImage(image, dst.x, dst.y);
					}
				}
			}
		};
	}

	public ImageLoader getImageLoader(SkinProperties properties) {
		ImageLoader loader = (ImageLoader) mapImageLoaders.get(properties);

		if (loader != null) {
			return loader;
		}

		loader = new ImageLoader(Display.getDefault(), properties);
		mapImageLoaders.put(properties, loader);

		return loader;
	}

	public void addToControlMap(SWTSkinObject skinObject) {
		String sID = skinObject.getSkinObjectID();
		if (DEBUGLAYOUT) {
			System.out.println("addToControlMap: " + sID + " : " + skinObject);
		}
		addToArrayMap(mapIDsToControls, sID, skinObject);

		// For SWT layout -- add a reverse lookup
		Control control = skinObject.getControl();
		if (control != null) {
			control.setData("ConfigID", skinObject.getConfigID());
			control.setData("SkinID", sID);
		}
	}

	private void addToArrayMap(Map arrayMap, Object key, SWTSkinObject object) {
		Object existing = arrayMap.get(key);
		if (existing instanceof SWTSkinObject[]) {
			SWTSkinObject[] existingObjects = (SWTSkinObject[]) existing;

			boolean bAlreadyPresent = false;
			for (int i = 0; i < existingObjects.length; i++) {
				//System.out.println(".." + existingObjects[i]);
				if (existingObjects[i].equals(object)) {
					bAlreadyPresent = true;
					System.err.println("already present: " + key + "; " + object
							+ "; existing: " + existingObjects[i]);
					break;
				}
			}

			if (!bAlreadyPresent) {
				int length = existingObjects.length;
				SWTSkinObject[] newObjects = new SWTSkinObject[length + 1];
				System.arraycopy(existingObjects, 0, newObjects, 0, length);
				newObjects[length] = object;

				arrayMap.put(key, newObjects);
				//				System.out.println("addToArrayMap: " + key + " : " + object + " #"
				//						+ (length + 1));
			}
		} else {
			arrayMap.put(key, new SWTSkinObject[] { object
			});
		}
	}

	private Object getFromArrayMap(Map arrayMap, Object key, SWTSkinObject parent) {
		if (parent == null) {
			return null;
		}

		SWTSkinObject[] objects = (SWTSkinObject[]) arrayMap.get(key);
		if (objects == null) {
			return null;
		}

		for (int i = 0; i < objects.length; i++) {
			SWTSkinObject object = objects[i];
			SWTSkinObject thisParent = object;
			while (thisParent != null) {
				if (thisParent.equals(parent)) {
					return object;
				}
				thisParent = thisParent.getParent();
			}
		}

		return null;
	}

	private void setSkinObjectViewID(SWTSkinObject skinObject, String sViewID) {
		addToArrayMap(mapPublicViewIDsToControls, sViewID, skinObject);
	}

	public void dumpObjects() {
		System.out.println("=====");
		FormData formdata;
		for (Iterator iter = mapIDsToControls.keySet().iterator(); iter.hasNext();) {
			String sID = (String) iter.next();
			Control control = getSkinObjectByID(sID).getControl();

			formdata = (FormData) control.getLayoutData();

			System.out.println(sID);

			sID += ".attach.";

			if (formdata.left != null) {
				System.out.println(sID + "left=" + getAttachLine(formdata.left));
			}
			if (formdata.right != null) {
				System.out.println(sID + "right=" + getAttachLine(formdata.right));
			}
			if (formdata.top != null) {
				System.out.println(sID + "top=" + getAttachLine(formdata.top));
			}
			if (formdata.bottom != null) {
				System.out.println(sID + "bottom=" + getAttachLine(formdata.bottom));
			}
		}
	}

	public SWTSkinObject getSkinObjectByID(String sID) {
		SWTSkinObject[] objects = (SWTSkinObject[]) mapIDsToControls.get(sID);
		if (objects == null) {
			return null;
		}

		return objects[0];
	}

	protected SWTSkinObject getSkinObjectByID(String sID, SWTSkinObject parent) {
		if (parent == null) {
			// XXX Search for parent is shell directly
			return getSkinObjectByID(sID);
		}

		return (SWTSkinObject) getFromArrayMap(mapIDsToControls, sID, parent);
	}

	public SWTSkinObject getSkinObject(String sViewID) {
		SWTSkinObject[] objects = (SWTSkinObject[]) mapPublicViewIDsToControls.get(sViewID);
		if (objects == null) {
			return null;
		}

		return objects[0];
	}

	public SWTSkinObject getSkinObject(String sViewID, SWTSkinObject parent) {
		if (parent == null) {
			// XXX Search for parent is shell directly
			return getSkinObject(sViewID);
		}

		return (SWTSkinObject) getFromArrayMap(mapPublicViewIDsToControls, sViewID,
				parent);
	}

	public SWTSkinTabSet getTabSet(String sID) {
		return (SWTSkinTabSet) mapTabSetToControls.get(sID);
	}

	public void activateTab(SWTSkinObject skinObjectInTab) {
		if (skinObjectInTab == null) {
			return;
		}

		for (Iterator iter = mapTabSetToControls.values().iterator(); iter.hasNext();) {
			SWTSkinTabSet tabset = (SWTSkinTabSet) iter.next();

			SWTSkinObjectTab[] tabs = tabset.getTabs();
			boolean bHasSkinObject = false;
			for (int i = 0; i < tabs.length; i++) {
				SWTSkinObjectTab tab = tabs[i];
				SWTSkinObject[] activeWidgets = tab.getActiveWidgets();
				for (int j = 0; j < activeWidgets.length; j++) {
					SWTSkinObject object = activeWidgets[j];
					//System.out.println("check " + tab + ";" + object + " for " + skinObjectInTab);
					if (hasSkinObject(object, skinObjectInTab)) {
						//System.out.println("FOUND");
						tabset.setActiveTab(tab);
						return;
					}
				}
			}
		}
		System.out.println("NOT FOUND");
	}

	private boolean hasSkinObject(SWTSkinObject start, SWTSkinObject skinObject) {
		if (start instanceof SWTSkinObjectContainer) {
			SWTSkinObject[] children = ((SWTSkinObjectContainer) start).getChildren();
			for (int i = 0; i < children.length; i++) {
				SWTSkinObject object = children[i];
				//System.out.println("  check " + object + " in " + start + " for " + skinObject);
				if (hasSkinObject(object, skinObject))
					return true;
			}
		}
		//System.out.println("  check " + start + " for " + skinObject);
		return skinObject.equals(start);
	}

	public SWTSkinTabSet getTabSet(SWTSkinObject skinObject) {
		String sTabSetID = skinObject.getProperties().getStringValue(
				skinObject.getConfigID() + ".tabset");
		return getTabSet(sTabSetID);
	}

	public boolean setActiveTab(String sTabSetID, String sTabID) {
		SWTSkinTabSet tabSet = getTabSet(sTabSetID);
		if (tabSet == null) {
			System.err.println(sTabSetID);
			return false;
		}

		return tabSet.setActiveTab(sTabID);
	}

	public void initialize(Shell shell, String startID) {

		this.shell = shell;
		FormLayout layout = new FormLayout();
		shell.setLayout(layout);
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT);

		String[] sMainGroups = skinProperties.getStringArray(startID + ".widgets");
		if (sMainGroups == null) {
			System.out.println("NO " + startID + ".widgets!!");
			sMainGroups = new String[] {};
		}

		for (int i = 0; i < sMainGroups.length; i++) {
			String sID = sMainGroups[i];

			if (DEBUGLAYOUT) {
				System.out.println("Container: " + sID);
			}

			linkIDtoParent(skinProperties, sID, sID, null, false, true);
		}
	}

	/**
	 * 
	 */
	private void addPaintListenerToAll(Control control) {
		// XXX: Bug: When paint listener is set to shell, browser widget will flicker on OSX when resizing
		if (!(control instanceof Shell)) {
			control.addListener(SWT.Paint, ontopPaintListener);
		}

		if (control instanceof Composite) {
			Composite composite = (Composite) control;

			Control[] children = composite.getChildren();
			for (int i = 0; i < children.length; i++) {
				addPaintListenerToAll(children[i]);
			}
		}
	}

	public void layout() {
		DEBUGLAYOUT = false;
		if (DEBUGLAYOUT) {
			System.out.println("==== Start Apply Layout");
		}
		// Apply layout data from skin
		for (Iterator iter = mapIDsToControls.keySet().iterator(); iter.hasNext();) {
			String sID = (String) iter.next();
			SWTSkinObject[] objects = (SWTSkinObject[]) mapIDsToControls.get(sID);

			if (DEBUGLAYOUT) {
				System.out.println("Apply Layout for " + objects.length + " " + sID);
			}

			for (int i = 0; i < objects.length; i++) {
				attachControl(objects[i]);
			}
		}

		for (Iterator iter = mapTabSetToControls.values().iterator(); iter.hasNext();) {
			SWTSkinTabSet tabset = (SWTSkinTabSet) iter.next();
			tabset.clean();
		}

		// Disabled due to Browser flickering
		//addPaintListenerToAll(shell);

		bLayoutComplete = true;
		if (DEBUGLAYOUT) {
			System.out.println("==== End Apply Layout");
		}
	}

	/**
	 * @param controlToLayout
	 * @param id
	 */
	private void attachControl(SWTSkinObject skinObject) {
		if (skinObject == null) {
			return;
		}

		Control controlToLayout = skinObject.getControl();

		if (controlToLayout == null) {
			return;
		}

		String sConfigID = skinObject.getConfigID();
		SWTSkinProperties properties = skinObject.getProperties();

⌨️ 快捷键说明

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