wangting
2024-09-27 a3e87f78ee262ca9bb7d9b0c997639d5f3295890
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.vci.client.uif.engine.client.tableArea;
 
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
 
import javax.swing.JToggleButton;
 
import com.vci.client.portal.utility.DataModelFactory;
import com.vci.client.portal.utility.PLDefination;
import com.vci.client.portal.utility.UITools;
import com.vci.client.ui.swing.VCISwingUtil;
import com.vci.client.ui.swing.components.VCIJButton;
import com.vci.client.ui.swing.components.VCIJPanel;
import com.vci.client.uif.actions.client.UIFUtils;
import com.vci.client.uif.engine.client.BasePanel;
import com.vci.client.uif.engine.client.IRegionPanel;
import com.vci.client.uif.engine.client.tableArea.action.EditInTableActionInPoint;
import com.vci.client.uif.engine.client.tableArea.editable.EditableTablePanel;
import com.vci.corba.common.VCIError;
import com.vci.corba.portal.data.PLAction;
import com.vci.corba.portal.data.PLCommandParameter;
import com.vci.corba.portal.data.PLTabButton;
 
public class TablePanelButtonAreaPanel extends BasePanel implements ActionListener{
 
    /**
     * 
     */
    private static final long serialVersionUID = 5729565844495488380L;
    private String tabId = "";
    private IRegionPanel ownerPanel = null;
 
    private Map<String, String> params = new HashMap<String, String>();
 
    public TablePanelButtonAreaPanel(String tabId, String type, String context,
            PLDefination defination, DataModelFactory factory,
            IRegionPanel ownerPanel) {
        super(factory, type, context, defination);
        this.tabId = tabId;
        this.ownerPanel = ownerPanel;
    }
 
    public void init(){
        setLayout(new WrapLayout(FlowLayout.LEFT));
        getButtonPanel();
        //this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    }
 
    private void getButtonPanel(){
        VCIJButton[] btns = getBtns();
        for (VCIJButton btn : btns) {
            if(EditInTableActionInPoint.EDIT_IN_TABLE.equals(btn.getActionCommand())){
                addEditButtons(this);
            } else {
                this.add(btn);
            }
        }
    }
 
    public String getTabId() {
        return tabId;
    }
 
    public void setTabId(String tabId) {
        this.tabId = tabId;
    }
 
    public IRegionPanel getOwnerPanel() {
        return ownerPanel;
    }
 
    public void setOwnerPanel(IRegionPanel ownerPanel) {
        this.ownerPanel = ownerPanel;
    }
 
    private VCIJButton[] btns = null;
    public VCIJButton[] getBtns() {
        if(btns == null){
            btns = getButtonsByTabPageId(getTabId(), ownerPanel);
        }
        return btns;
    }
 
    public void setBtns(VCIJButton[] btns) {
        this.btns = btns;
    }
    
    
    private TablePanel tablePanel = null;
    private JToggleButton btnStartEditor = new JToggleButton("开始编辑", VCISwingUtil.createImageIcon("lock_open.png"));
    private VCIJButton btnClearSelected = VCISwingUtil.createVCIJButton("clearSelected", "清除已选", "", "clear.gif", this);
    private VCIJButton btnStopEditor = VCISwingUtil.createVCIJButton("endEditor", "结束编辑", "", "lock_edit.png", this);
    private void addEditButtons(VCIJPanel pal){
        // 当前只在TablePanel上支持编辑
        if(ownerPanel instanceof TablePanel){
            //初始化参数列表
            initParams();
            // 标识支持编辑
            setEditable(true);
            btnStartEditor.setActionCommand("startEditor");
            btnStartEditor.addActionListener(this);
            tablePanel = (TablePanel)ownerPanel;
            if(tablePanel.isSupportEditTableCell()){
                pal.add(btnStartEditor);
                pal.add(btnClearSelected);
                pal.add(btnStopEditor);
                // ‘清除’、‘结束编辑’ 正常情况下,不可用
                btnClearSelected.setEnabled(false);
                btnStopEditor.setEnabled(false);
            }
        }
    }
 
    private void initParams() {
        String oid = ownerPanel.getPageDefinition().plOId;
        try {
            PLTabButton[] buttons = UITools.getService().getPLTabButtonsByTableOId(oid);
            String buttonOid = "";
            for(int i = buttons.length - 1; i >= 0; i--) {
                PLAction action = UITools.getService().getPLActionById(buttons[i].plActionOId);
                if(action != null && 
                        EditInTableActionInPoint.EDIT_IN_TABLE.equals(action.plCode)) {
                    buttonOid = buttons[i].plOId;
                }
            }
            PLCommandParameter[] btnParams = UITools.getService().getPLCommandParametersByCommandOId(buttonOid);
            for(PLCommandParameter cp : btnParams){
                String key = cp.plKey;
                String value = cp.plValue;
                params.put(key, value);
            }
        } catch (VCIError e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        if(actionCommand.equals(btnStartEditor.getActionCommand())) startEditor();
        else if(actionCommand.equals(btnClearSelected.getActionCommand())) clearSelected();
        else if(actionCommand.equals(btnStopEditor.getActionCommand())) endEditor(); 
    }
    
    private void clearSelected(){
        if(tablePanel.getDataTablePanel().getTable().getCellEditor() != null){
            tablePanel.getDataTablePanel().getTable().getCellEditor().cancelCellEditing();
        }
        tablePanel.getDataTablePanel().clearCheckboxSelected();
    }
    private boolean editable = false;
    public boolean isEditable() {
        return editable;
    }
    public void setEditable(boolean editable) {
        this.editable = editable;
    }
 
    /**
     * 是否在编辑中
     */
    private boolean editing = false;
    public boolean isEditing() {
        return editing;
    }
    public void setEditing(boolean editing) {
        this.editing = editing;
    }
 
    private void startEditor(){
        // 当进入可编辑状态后,‘清除’、‘结束编辑’才可用
        tablePanel.getDataTablePanel().setCellEditable(true);        
        tablePanel.getDataTablePanel().setLastClickRowToChecked(false);
        btnStartEditor.setEnabled(false);
        btnStartEditor.setSelected(true);
        btnClearSelected.setEnabled(true);
        btnStopEditor.setEnabled(true);
        setEditing(true);
    }
    
    @SuppressWarnings("deprecation")
    private void endEditor(){
        try{
            // 调用 结束编辑后的事件处理逻辑(内部判断是否发生了变更)
            if (tablePanel.getDataTablePanel().getTable().isEditing()) {
                tablePanel.getDataTablePanel().getTable().getCellEditor().stopCellEditing();
            }
            saveChange();
        }catch(Exception e){
            //TODO 异常处理
            e.printStackTrace();
            UIFUtils.showErrorMessage(tablePanel, e);
        } finally{
            // 当结束编辑后,‘清除’、‘结束编辑’还原为不可用,
            tablePanel.getDataTablePanel().setCellEditable(false);
            tablePanel.getDataTablePanel().setLastClickRowToChecked(true);
            btnStartEditor.setEnabled(true);
            btnStartEditor.setSelected(false);
            btnClearSelected.setEnabled(false);
            btnStopEditor.setEnabled(false);
            setEditing(false);
        }
    }
    
    private void saveChange() throws Exception{
        if(tablePanel instanceof EditableTablePanel){
            ((EditableTablePanel)tablePanel).saveChange(params);
        }
    }
}