wangting
2024-12-26 fa261e8c1220b31af54e8167e4de9c3320b1af27
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package com.vci.client.uif.engine.client;
 
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
 
import com.vci.client.LogonApplication;
import com.vci.client.framework.delegate.RoleRightClientDelegate;
import com.vci.client.framework.delegate.SystemCfgClientDelegate;
import com.vci.client.framework.rightdistribution.object.RoleRightObject;
import com.vci.client.framework.util.RightControlUtil;
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.exception.VCIException;
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.BOAFactory;
import com.vci.client.uif.engine.client.tableArea.ButtonActionListener;
import com.vci.corba.common.VCIError;
import com.vci.corba.portal.data.PLAction;
import com.vci.corba.portal.data.PLTabButton;
import com.vci.mw.ClientContextVariable;
 
public class BasePanel extends VCIJPanel{
 
    /**
     * 
     */
    private static final long serialVersionUID = -160889706670945852L;
    private DataModelFactory factory = null;
    private String type = "";
    private String context = "";
    private PLDefination defination;
    public BasePanel(){
        
    }
    public BasePanel(DataModelFactory factory, String type, String context,
            PLDefination defination) {
        super();
        this.factory = factory;
        this.type = type;
        this.context = context;
        this.defination = defination;
    }
    public DataModelFactory getFactory() {
        return factory;
    }
    public void setFactory(DataModelFactory factory) {
        this.factory = factory;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getContext() {
        return context;
    }
    public void setContext(String context) {
        this.context = context;
    }
    public PLDefination getDefination() {
        return defination;
    }
    public void setDefination(PLDefination defination) {
        this.defination = defination;
    }
    
    public VCIJPanel addButtonsToSelf(String tabId, IRegionPanel regionPanel){
        VCIJButton[] btns = getButtonsByTabPageId(tabId, regionPanel);
        addButtonsToPanel(this, btns);
        return this;
    }
    public VCIJPanel addButtonsPanel(String tabId, IRegionPanel regionPanel, VCIJPanel pal){
        VCIJButton[] btns = getButtonsByTabPageId(tabId, regionPanel);
        pal = addButtonsToPanel(pal, btns);
        return pal;
    }
    private VCIJPanel addButtonsToPanel(VCIJPanel pal, VCIJButton[] btns){
        pal.setLayout(new FlowLayout(FlowLayout.LEADING));
        for (VCIJButton btn : btns) {
            pal.add(btn);
        }
        return pal;
    }
    private Comparator<PLTabButton> COMPARABLE_COMAPRATOR = new Comparator<PLTabButton>() {
        //随第一个参数小于、等于或大于第二个参数而分别返回负整数、零或正整数。
        public int compare(PLTabButton o1, PLTabButton o2) {
            int res = -1;
            if(o1.plSeq < o2.plSeq) res = -1;
            else if(o1.plSeq == o2.plSeq) res = 0;
            else if(o1.plSeq > o2.plSeq) res = 1;
            return res;
        }
    };
 
    private Comparator<RoleRightObject> COMPARABLE_COMAPRATOR_RRO = new Comparator<RoleRightObject>() {
         @SuppressWarnings("unchecked")
        //随第一个参数小于、等于或大于第二个参数而分别返回负整数、零或正整数。
        public int compare(RoleRightObject o1, RoleRightObject o2) {
            int res = -1;
            if(o1.getRightValue() < o2.getRightValue()) res = -1;
            else if(o1.getRightValue() == o2.getRightValue()) res = 0;
            else if(o1.getRightValue() > o2.getRightValue()) res = 1;
            return res;
        }
    };
    /**
     * 根据TagId、IRegionPanl 返回Tab所在Definition定义的Buttons
     * @param tabId tabPageId
     * @param regionPanel IRegionPanel 
     * @see IRegionPanel
     * @return
     * @throws  
     */
    public VCIJButton[] getButtonsByTabPageId(String tabId, IRegionPanel regionPanel) {
        VCIJButton[] btns = new VCIJButton[0];
        List<VCIJButton> btnList = new LinkedList<VCIJButton>();
        try {
            RoleRightClientDelegate delegate = new RoleRightClientDelegate();
            PLTabButton[] buttons = new PLTabButton[0];
            String pageDefId = regionPanel.getPageDefinition().plOId;
            LinkedList<Long> rightValueList = new LinkedList<Long>();
 
            String uiRightSwith = new SystemCfgClientDelegate().getConfigValue("ui.right.swith");
            // UI权限开关是否关闭
            boolean uiRightSwithIsOff = "off".equalsIgnoreCase(uiRightSwith);
            
            if(RightControlUtil.isAdminOrDeveloperOrRoot(ClientContextVariable.getInvocationInfo().userName)){
                // 显示标识UI权限开关为关闭
                buttons = UITools.getService().getPLTabButtonsByTableOId(pageDefId);
                uiRightSwithIsOff = true;
            } else {
                // 当前用户拥有的角色对于 pageDefId 对应的模块 所拥有的角色权限数据
                RoleRightObject[] rights = delegate.getRoleRightByModule(
                        pageDefId, ClientContextVariable.getInvocationInfo().userName, LogonApplication.currentUserRoleRights);
                // 角色对于该模块没有权限,直接返回
                if(rights.length <= 0) {
                    return btns;
                }
                for(RoleRightObject rro : rights){
                    long rightValue = rro.getRightValue();
                    rightValueList.add(rightValue);
                }
                // 根据角色权限(值)进行小-》大排序,取权限值最大的有效
                Arrays.sort(rights, COMPARABLE_COMAPRATOR_RRO);
                long right = rights[rights.length - 1].getRightValue();
                if (right == 0) {
                    return btns;
                }
                // 查看模块上定义的按钮
                buttons = UITools.getService().getPLTabButtonsByTableOId(pageDefId);
            }
            // 按钮排序
            Arrays.sort(buttons, COMPARABLE_COMAPRATOR);
            if (buttons.length < 1) {
                return btns;
            }
            
            for (int i = 0; i < buttons.length; i++) {
                if(buttons[i].show.equals("0")){
                    if(buttons[i].authorization.equals("0")){
                        if (uiRightSwithIsOff || hasRight(rightValueList, buttons[i].plSeq)) {
                            PLAction action = UITools.getService().getPLActionById(buttons[i].plActionOId);
                            VCIJButton btn = getVCIJButton(buttons[i], regionPanel, action);
                            btnList.add(btn);
                        }
                    } else {
                        PLAction action = UITools.getService().getPLActionById(buttons[i].plActionOId);
                        VCIJButton btn = getVCIJButton(buttons[i], regionPanel, action);
                        btnList.add(btn);
                    }
                }
            }
        } catch (VCIError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (VCIException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return btnList.toArray(new VCIJButton[]{});
    }
    
    public VCIJButton getVCIJButton(PLTabButton button, IRegionPanel regionPanel, PLAction action) {
        VCIJButton btn = new VCIJButton(button.plLabel);
        btn.setActionCommand(action.plCode);
        if(button.plDesc != null && !"".equals(button.plDesc)){
            btn.setToolTipText(button.plDesc);
        }
        ButtonActionListener bal = new ButtonActionListener(
                getType(), getContext(), button, action, regionPanel,
                btn);
        String icon = BOAFactory.getActionIcon(action.plCode);
        // add by xchao 2015.07.16 end
        // 增加按钮图标解析,支持定义及参数定义
        String iconPath = button.iconPath;
        Map<String, String> paramMap = bal.buildActionParamsMap();
        
        // 按钮上直接定义的图标
        if(iconPath == null || "".equals(iconPath) || "undefined".equals(iconPath)){
            // 如果直接定义的为空,则尝试从按钮参照上取
            if(paramMap.containsKey("iconPath")){
                icon = paramMap.get("iconPath");
                // 按钮参照上的也空为,则取默认规则处理的图标
                if(icon == null || "".equals(icon)){
                    icon = BOAFactory.getActionIcon(action.plCode);
                }
            }
        } else {
            icon = iconPath;
        }
        // add by xchao 2015.07.16 end
        if(icon != null && !"".equals(icon)){
            try{
                btn.setIcon(VCISwingUtil.createImageIcon(icon));
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        btn.addActionListener(bal);
        //add by guo 设置button的大小
        Dimension btnSize = btn.getPreferredSize();//btn的显示参数
        if(paramMap.containsKey("bheight")){
            int bheight = parseInt(paramMap.get("bheight"), btnSize.height);
            btnSize.height = bheight;
        }
        if(paramMap.containsKey("bwidth")){
            int bwidth = parseInt(paramMap.get("bwidth"), btnSize.width);
            btnSize.width = bwidth;
        }
        btn.setPreferredSize(btnSize);
        //add by guo end
        return btn;
    }
    
    private int parseInt(String value, int num) {
        int tvalue = num;
        try {
            tvalue = Integer.parseInt(value);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return tvalue;
    }
 
    private boolean hasRight(List<Long> rightValueList,int operNum){
        boolean res = false;
        for(Long rightValue : rightValueList){
            res = hasRight(rightValue, operNum);
            if(res){
                break;
            }
        }
        return res;
    }
    private boolean hasRight(long rightValue,int operNum){
        boolean res = false;
        long preValue = (rightValue >> operNum) & 1;
        //int preValue = (int)Math.pow(2, operNum);
        //if(preValue == (rightValue & preValue)){
        if (preValue == 1) {
            res = true;
        }
        return res;
    }
}