?? setcommand.java
字號:
/** * SetCommand.java * Copyright 2005 Carlos Silva A. * * 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 jgantt.cmds;import java.lang.reflect.Method;import java.text.MessageFormat;import java.util.Date;import jgantt.model.Task;/** * SetCommand represents a command that executes a setter for an object. Uses * reflextion to invoke the method with diferent attributes. Usage:new * SetCommand(Object, "setMethod", executeValue, undoValue)); * <p> * $Date: 2005/07/14 06:16:04 $ * </p> * * @version $Revision: 1.4 $ * @author Carlos Silva */public class SetCommand implements Command { String setter; Class[] argType; Object oldValue; Object newValue; Object object; private SetCommand(Object object, String setter) { this.object = object; this.setter = setter; } /** * Asignador de String */ public SetCommand(Object object, String setter, String newVal, String oldVal) { this.object = object; argType = new Class[] { String.class }; this.newValue = newVal; this.oldValue = oldVal; } /** * Asignador de int */ public SetCommand(Task task, String setter, int newValue, int oldValue) { this(task, setter); argType = new Class[] { Integer.TYPE }; this.newValue = new Integer(newValue); this.oldValue = new Integer(oldValue); } /** * Asignador de boolean * * @param task * @param setter * @param newValue * @param oldValue */ public SetCommand(Task task, String setter, boolean newV, boolean oldV) { this(task, setter); argType = new Class[] { Boolean.TYPE }; this.newValue = new Boolean(newV); this.oldValue = new Boolean(oldV); } /** * Asignador de boolean * * @param task * @param setter * @param newValue * @param oldValue */ public SetCommand(Task task, String setter, Date newV, Date oldV) { this(task, setter); argType = new Class[] { Boolean.TYPE }; this.newValue = newV; this.oldValue = oldV; } /** * @see jgantt.cmds.TaskCommand#execute() */ public void execute() { invoke(newValue); } /** * @see jgantt.cmds.TaskCommand#undo() */ public void undo() { invoke(oldValue); } void invoke(Object value) { try { Method method = object.getClass().getMethod(setter, argType); method.invoke(object, new Object[] { value }); } catch (Exception e) { Object a[] = new Object[] { setter, argType[0], value }; String spec = MessageFormat.format("invoke ${0}(${1}, ${2})", a); System.err.println(spec); e.printStackTrace(); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -