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

📄 window.java

📁 Micro Window Toolkit(MWT)是一个用于开发J2ME用户界面(UI)的工具包。它具有友好
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			
			if(!processKeyEvents || focus == null) return;
			if(!focus.acceptsFocus()) this.setFocusNext();
			if(focus == null) return;
			
//			2.0
//			boolean result;
//			int key = (int)keys[index];
//			int pressed = (int)(keys[index] >> 32);
//			switch(pressed) {
//				case 0: result = focus.keyUp(key, this); break;
//				case 1: result = focus.keyDown(key, this); break;
//				default: result = focus.keyPress(key, pressed, this); break;
//			}
//			if(!result) dispatchKey(keys[index]);
			
			if(!focus.keyEvent(keys[index],this)) dispatchKey(keys[index]);
		}
	}
	
	/**
	 * Increments the state for all the keys that are not released.<br>
	 * This method triggers component's key events if processKeyEvents is true.
	 * @param processKeyEvents True if component's key events should be triggered
	 */
	final public void repeatKeys(boolean processKeyEvents) {
		if(dialog != null) { dialog.repeatKeys(processKeyEvents); return; }
		
		synchronized(keys) {
			for(int i=0; i<keysLength ;i++)
				if((keys[i] >> 32) != 0) setKeyState((int)keys[i],KEYSTATE_REPEATED,processKeyEvents);
		}
	}


	final static private int WAIT = 750;
	final static private int INTERVAL = 200;
	static private long lastTime;
	static private long interTime;
	/**
	 * This method is called whenever a key event is not consumed by the focused component.<br>
	 * The default implementation moves the focus.
	 */	
	//@see Component#keyEvent(long, Window)
	protected void dispatchKey(long key) {
		if((key >> 32) == 1) {
			lastTime = System.currentTimeMillis();
			interTime = 0;
		}
		else {
			if(System.currentTimeMillis() - lastTime < WAIT) return;
			if(System.currentTimeMillis() - interTime < INTERVAL) return;
			interTime = System.currentTimeMillis();
		}
		switch(getFocusAction((int)key)) {
			case FOCUSACTION_PREV: setFocusPrevious(); break;
			case FOCUSACTION_NEXT: setFocusNext(); break;
		}
	}
	
	/**
	 * Paints the window and all its childs components into the given graphics object.
	 * @param g The graphics object
	 */
	final public void paint(Graphics g) {
		rectX = g.getTranslateX();
		rectY = g.getTranslateY();
		rectW = g.getClipWidth();
		rectH = g.getClipHeight()+1;
//		rectX = getX();
//		rectY = getY();
//		rectW = getWidth();
//		rectH = getHeight()+1;
		
		this._paint(g,this);
		if(dialog != null)
			dialog.paint(g);
	} 
	
	
	// overrides
	protected void paint(Graphics g, Window window) {
		getSkin(isHierarchyEnabled()? 0 : 1).paint(this,g);
		paintChilds(g,window);
	}
	
	
	/**
	 * Opens the given window as a dialog.
	 * @param dialog
	 * @see <a href="dialogs">Dialogs</a>
	 */
	final public void dialogOpen(Window dialog) {
		synchronized(keys) {
			synchronized(dialog.keys) {
				dialog.keys = keys;
				dialog.keysLength = keysLength;
			}
		}
		this.setEnabled(false);
		this.dialog = dialog;
	}
	
	/** Gets this window's current dialog.
	 * @see <a href="dialogs">Dialogs</a> */ 
	public Window getDialog() { return dialog; }
	
	/**
	 * Closes the current dialog.
	 * @see <a href="dialogs">Dialogs</a>
	 */
	final public void dialogClose() {
		if(dialog == null) return;
		if(dialog.dialog != null) dialog.dialogClose();
		synchronized(keys) {
			synchronized(dialog.keys) {
				if(keysLength < dialog.keysLength)
					keysLength = dialog.keysLength;
			}
		}
		this.setEnabled(true);
		dialog = null;
	}	

	/** Gets the current focused component. */
	final public Component getFocus() { return focus; }
	
	/** Sets the focus to the first component that accepts focus.
	 *  @see Component#acceptsFocus() */
	final public void setFocusFirst() { moveFocus(this,true);	}
	
	/** Focus the next component that accepts focus.
	 *  @see Component#acceptsFocus() */
	final public void setFocusNext() { if(focus != null) moveFocus(focus,true);  }
	
	/** Focus the previous component that accepts focus.
	 *  @see Component#acceptsFocus() */
	final public void setFocusPrevious() { if(focus != null) moveFocus(focus,false); }
	
	/**
	 * Sets the focus to the given component.<br>
	 * If the given component doesn't accept focus, this does nothing.<br>
	 * Null is a valid parameter.
	 */
	final public void setFocus(final Component c) {
		if(c == null || c.acceptsFocus()) {
			final Component t = focus;
			focus = c;
//			if(t != null) t.blur();
//			if(focus != null && focus != t) focus.focus();
			if(t != null) t.focusLost();
			if(focus != null && focus != t) focus.focusGained();
		}
	}
	
	
	// Returns the index for the given keyCode in the keys array (binary search)
	// If there's not such keyCode, added it.
	final private int getKeyIndex(int keyCode) {
		if(dialog != null) { return dialog.getKeyIndex(keyCode); }
		synchronized(keys) {
		    int high = keysLength, low = -1, probe;
		    while(high - low > 1) {
		        probe = (high + low) / 2;
		        if((int)keys[probe] < keyCode) low = probe;
		        else high = probe;
		    }
		    if(!(high == keysLength || (int)keys[high] != keyCode)) return high;

			if(keys.length == keysLength) {
				final long[] old = keys;
				keys = new long[keys.length + KEYS_INC];
				System.arraycopy(old,0,keys,0,old.length);
			}
			for(int i=0; i<keysLength ;i++)
				if(keyCode < (int)keys[i]) {
					System.arraycopy(keys,i,keys,i+1,keysLength-i);
					keys[i] = (long) keyCode;
					keysLength++;
					return i;
				}
			keys[keysLength] = (long) keyCode;
			keysLength++;
			return keysLength-1;
		}
	}

	
	// ==========
	// Move Focus
	// ==========
	// This algorithm is used to move the focus from the given component
	// forward or backward in this tree data structure.
	// Very dirty, but very fast.
	private Component start;	// the start component
	private boolean first;		// true durning the first call of findNeighbor
	private boolean forward = true; // if should go forward or backward (TODO why is true?)
	
	// This dummy component is returned in case there's not match
	final static private Component fail = new Component(0,0,0,0,false);
	
	// An int value is used by findNeighbor, if it's DONT_GO_UP, then it should find
	// the childs from the given component, but never its parents.
	// If it's SCAN_ALL, it can go everywhere.
	// Other values stands for the index of the next child component.
	final static private int DONT_GO_UP = -3;
	final static private int SCAN_ALL = -4;
	
	final private void moveFocus(Component c, boolean forward) {
		start = c;
		first = true;
		this.forward = forward;
		final Component t = focus; // temp
		
		focus = findNeighbor(c,SCAN_ALL);
		if(focus == fail) focus = null;
		start = null;
	
//		if(t != null) t.blur();
//		if(focus != null && focus != t) focus.focus();
		if(t != null) t.focusLost();
		if(focus != null && focus != t) focus.focusGained();
	}
	
	final private Component findNeighbor(Component c, int index) {
		// skips the first check
		if(first) first = false;
		else {
			if(c.acceptsFocus()) return c;
			if(c == start)  return fail;
		}
		// -1 means never go up
		final int start;
		final int finish;
		final int inc;
		if(forward) {
			start = (index < 0)? 0 : index;
			finish = c.childs.size();
			inc = 1;
		}
		else {
			start = (index < -1)? c.childs.size()-1 : index;
			finish = -1;
			inc = -1;
		}
		for(int i=start; i!=finish ;i+=inc) {
			final Component res = findNeighbor((Component) c.childs.elementAt(i),DONT_GO_UP);
			if(res != null) return res;
		}
		// the parent was checked already
		if(index == DONT_GO_UP) return null;
		// if it's the root, start again
		if(c.getParent() == null) return findNeighbor(c,SCAN_ALL);
		// go up
		return findNeighbor(c.getParent(),c.getParent().childs.indexOf(c)+((forward)? 1 : -1));
	}	
}

⌨️ 快捷键说明

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