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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package com.vci.client.uif.engine.client.tableArea;
 
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swing.ComboBoxModel;
import javax.swing.JPanel;
import javax.swing.text.JTextComponent;
 
import com.vci.client.portal.utility.DataModelFactory;
import com.vci.client.portal.utility.PLDefination;
import com.vci.client.portal.utility.PRM;
import com.vci.client.portal.utility.PRMItem;
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.VCIJComboBox;
import com.vci.client.ui.swing.components.VCIJLabel;
import com.vci.client.ui.swing.components.VCIJPanel;
import com.vci.client.ui.swing.components.VCIJScrollPane;
import com.vci.client.ui.swing.components.VCIJTextField;
import com.vci.client.uif.engine.client.controls.ControlFactory;
import com.vci.client.uif.engine.client.controls.DateControl;
import com.vci.client.uif.engine.client.controls.DateTimeControl;
import com.vci.client.uif.engine.client.controls.ICustomControl;
import com.vci.client.uif.engine.client.controls.TimeControl;
import com.vci.client.uif.engine.client.controls.VCIJComboBoxModelValueObject;
import com.vci.corba.portal.data.PortalVI;
import com.vci.mw.ClientContextVariable;
 
public class TablePanelSearchAreaPanel extends JPanel implements ActionListener{
 
    /**
     * 
     */
    private static final long serialVersionUID = -980328802916633426L;
 
    private String type = "";
    private String context = "";
    private PLDefination defination = null;
    private DataModelFactory factory = null;
    private TablePanel tablePanel = null;
 
    private PortalVI tableDef = null;
    private PRM tablePrm = null;
    private PortalVI formView = null;
    private PRM formPrm = null;
    
    private VCIJButton btnSearch = VCISwingUtil.createVCIJButton("search", "查询", "查询", "search.png", this);
    private VCIJButton btnSearchAdvance = VCISwingUtil.createVCIJButton("search_advance", "高级查询", "点击开始高级查询", "search_advance.png", this);
    private VCIJButton btnClear = VCISwingUtil.createVCIJButton("clear_search", "清空条件", "清空条件", "clear.gif", this);
    private VCIJButton btnClearaAndReload = VCISwingUtil.createVCIJButton("clear_search_reload", "清空条件并重新加载", "清空后重新加载", "pagination_refresh.png", this);
    
    private HashMap<String, VCIJLabel> ctlLblMap = new HashMap<String, VCIJLabel>();
    private HashMap<String, Component> ctlComptMap = new HashMap<String, Component>();
    
    private boolean hasNormatSearchAttrs = true;
    private boolean hasAdvanceSearchAttrs = false;
    
    public TablePanelSearchAreaPanel(String type, String context,
            PLDefination defination, DataModelFactory factory,
            TablePanel tablePanel) {
        
        super();
        
        this.type = type;
        this.context = context;
        this.defination = defination;
        
        this.factory = factory;
        this.tablePanel = tablePanel;
    }
    
    public void init(){
        setLayout(new BorderLayout());
        try{
            add(new VCIJScrollPane(getSearchFormPanel()), BorderLayout.CENTER);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        add(getSouthButtonPanel(), BorderLayout.SOUTH);
    }
    
 
    private VCIJPanel getSearchFormPanel() throws Throwable{
        VCIJPanel pal = new VCIJPanel(new GridBagLayout());
        
        tableDef = getPortalDefination();
        tablePrm = UITools.getPRM(tableDef.prm);
        boolean bShowSearch = tablePrm.getPrmItemList().get(0).getItemIsNavigatorExpand().equalsIgnoreCase("true");
        
        tablePanel.setExpandSearchArea(bShowSearch);
        
        formView = factory.getFormViewById(tablePrm.getPrmItemList().get(0).getItemInObj());
        formPrm = UITools.getPRM(formView.prm);
        boolean hasNormanSearchAttrs = hasNormalSearchAttrs();
        boolean hasAdvanceSearchAttrs = hasAdvanceSearchAttrs();
        setHasNormatSearchAttrs(hasNormanSearchAttrs);
        setHasAdvanceSearchAttrs(hasAdvanceSearchAttrs);
        if(!hasNormanSearchAttrs) return pal;
        
        String[] queryCols = tablePrm.getPrmItemList().get(0).getItemKeyFields().split(",");
        if (queryCols.length == 0) {
            return pal;
        }
        int gridx = 0;
        int gridy = 0;
        int gridwidth = 1;
        
        for (int i = 0; i < queryCols.length; i++) {
             for (int j = 0; j < formPrm.getPrmItemList().size(); j++) {
                 PRMItem prmItem = formPrm.getPrmItemList().get(j);
                 String field = prmItem.getItemField();
                 String name = prmItem.getItemName();
                 
                if (!queryCols[i].equalsIgnoreCase(field)) {
                    continue;
                }
                
                VCIJLabel attarLabel = new VCIJLabel(name + ":");
                attarLabel.setObj(prmItem);
                
                Component compt = ControlFactory.createControlForSearch(prmItem);
                if (compt == null)
                    continue;
                
                if(ControlFactory.isLCStatusAttr(field) && compt instanceof VCIJComboBox){
                    String btmType = defination.getShowType();
                    ((VCIJComboBox)compt).setModel(ControlFactory.getLCStatusComboboxModel(prmItem, btmType));
                }
                Component comptToAdd = compt;
                if(compt instanceof VCIJTextField){
                    VCIJTextField txt = ((VCIJTextField)compt); 
                    txt.setRequired(false);
                    txt.setEditable(true);
                }
                
                String key = field;
                ctlLblMap.put(key, attarLabel);
                ctlComptMap.put(key, compt);
                
                pal.add(attarLabel,
                        getGBC(gridx, gridy, 1, 1, 0.0, 0.0,
                                GridBagConstraints.NORTHEAST,
                                GridBagConstraints.NONE));
                gridx++;
                
                if (gridx == 3) {
                    gridwidth = GridBagConstraints.REMAINDER;
                }
 
                pal.add(
                        comptToAdd,
                        getGBC(gridx, gridy, gridwidth, 1,
                                1.0, 0.0, GridBagConstraints.NORTHWEST,
                                GridBagConstraints.BOTH));
                gridx++;
                if (gridx > 3) {
                    gridx = 0;
                    gridy += 1;
                    gridwidth = 1;
                }
            
             }
        }
        return pal;
    }
    
    private GridBagConstraints getGBC(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill) {
        int padxy = 2;
        return new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, new Insets(padxy, padxy, padxy, padxy), padxy, padxy);
    }
    private PortalVI getPortalDefination() throws Throwable{
        if(tableDef != null) return tableDef;
        if(defination.getLinkType() != null && !"".equals(defination.getLinkType())) {
            tableDef = factory.getTableDefinationByTypeAndTableName(defination.getLinkType(), defination.getTemplateId());
        } else {
            tableDef = factory.getTableDefinationByTypeAndTableName(defination.getShowType(), defination.getTemplateId());
        }
        return tableDef;
    }
 
    private VCIJPanel getSouthButtonPanel(){
        VCIJPanel pal = new VCIJPanel();
        boolean hasNormanSearchAttrs = isHasNormatSearchAttrs();
        boolean hasAdvanceSearhAttrs = isHasAdvanceSearchAttrs();
        if(hasNormanSearchAttrs){
            pal.add(btnSearch);
        }
        if(hasAdvanceSearhAttrs){
            pal.add(btnSearchAdvance);
        }
        if(hasNormanSearchAttrs || hasAdvanceSearhAttrs){
            pal.add(btnClear);
            pal.add(btnClearaAndReload);
        }
        return pal;
    }
 
    @Override
    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        if(actionCommand.equals(btnSearch.getActionCommand())) search();
        else if(actionCommand.equals(btnSearchAdvance.getActionCommand())) searchAdvance();
        else if(actionCommand.equals(btnClear.getActionCommand())) clear(false);
        else if(actionCommand.equals(btnClearaAndReload.getActionCommand())) clear(true);
    }
    
    private void search(){
        this.tablePanel.getDataTablePanel().setPageIndex(1);
        tablePanel.getDataProvider().setLoadByCondition(true);
        tablePanel.getDataProvider().setNormalSearchFilterMap(getSearchConditionMap(false));
        tablePanel.getDataTablePanel().refreshTableData();
    }
    
    private void searchAdvance(){
        TablePanalAdvanceSearchDialog ad = new TablePanalAdvanceSearchDialog(type, context, defination, factory, tablePanel);
        ad.init();
        ad.setSize(600, 400);
        ad.setLocationRelativeTo(ClientContextVariable.getFrame());
        ad.setVisible(true);
    }
    
    private void clear(boolean reload){
//        if(ctlComptMap.size() == 0) return;
        
        getSearchConditionMap(true);
        
        tablePanel.getDataProvider().setLoadByCondition(false);
        tablePanel.getDataProvider().getNormalSearchFilterMap().clear();
        
        if(reload){
            tablePanel.getDataTablePanel().refreshTableData();
        }
    }
    private HashMap<String, String> getSearchConditionMap(boolean isClearn){
        HashMap<String, String> map = new HashMap<String, String>();
        Iterator<String> keys = ctlComptMap.keySet().iterator();
        while(keys.hasNext()){
            String key = keys.next();
            Component compt = ctlComptMap.get(key);
            String value = "";
            if(JTextComponent.class.isAssignableFrom(compt.getClass())){
                JTextComponent txtCompt = (JTextComponent)compt;
                String text = txtCompt.getText();
                if(isClearn){
                    text = "";
                }
                txtCompt.setText(text);
 
                if(text.contains("%")){
                    text = text.replace("%", "*");
                }
//                if(text.contains("*")){
//                    if(!text.startsWith("*")){
//                        text = "*" + text;
//                    }
//                    if(!text.endsWith("*")){
//                        text = text + "*";
//                    }
//                }
                value = text;
            } else if(compt instanceof VCIJComboBox){
                VCIJComboBox cbx = (VCIJComboBox)compt;
                ComboBoxModel model = cbx.getModel();
                Object selectObj = model.getSelectedItem();
                if(selectObj instanceof VCIJComboBoxModelValueObject){
                    VCIJComboBoxModelValueObject vo  = (VCIJComboBoxModelValueObject) selectObj;
                    value = vo.getValue();
                    if(isClearn) {
                        try {
                            cbx.setSelectedIndex(0);
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }else if(compt instanceof ICustomControl){
                ICustomControl ctrl = (ICustomControl)compt;
                value = ctrl.getValue();
                if(!"".equals(value) && isDateTimeControl(compt)){
                    //modify by songyf 2014.09.05 修改tabel中使用时间作为查询条件异常
                    // 不需要再此处转换,后台自动判断
                    //value = "to_date('" + value + "','YYYY-MM-dd HH24:mi:ss')";
                    //end
                    if(isClearn){
                        ((DateTimeControl)compt).clearValue();
                    }
                }
            }
            if(isClearn){
                value = "";
            }
            map.put(key, value);
        }
        return map;
    }
    
    private boolean isDateTimeControl(Component compt){
        boolean res = false;
        if(compt instanceof DateControl || compt instanceof TimeControl || compt instanceof DateTimeControl){
            res = true;
        }
        return res;
    }
 
 
    /**
     * 返回 table 是否定义了一般查询属性
     * @return
     */
    private boolean hasNormalSearchAttrs(){
        boolean res = true;
        String[] queryCols = tablePrm.getPrmItemList().get(0).getItemKeyFields().split(",");
        int len = queryCols.length;
        if(len <= 0) return false;
        res = false;
        for(String queryCol : queryCols){
            if(queryCol != null && !"".equals(queryCol)){
                res = true;
            }
            if(res){
                break;
            }
        }
        return res;
    }
    
    /**
     * 返回 table 是否定义了高级查询属性
     * @return 
     */
    private boolean hasAdvanceSearchAttrs(){
        boolean res = true;
        if(tableDef != null){
            PRM tablePrm = UITools.getPRM(tableDef.prm);
            if(tablePrm != null){
                Map<String, Integer> asvMap = tablePrm.getPrmItemList().get(0).getColAndUseCountMap();
                if(asvMap == null || asvMap.size() < 1) return false;
                // 先置为false
                res = false;
                // 实际上只需要判断 asvMap.size() 是否大于0即可,
                // 但由于定义时偶尔会出现一条空数据问题,因此增加下面的判断
                // 只有有一个有效的高级查询属性定义,则返回true
                Iterator<String> its = asvMap.keySet().iterator();
                while(its.hasNext()){
                    String key = its.next();
                    if(key != null && !"".equals(key)){
                        res = true;
                    }
                    if(res){
                        break;
                    }
                }
            }
        }
        return res;
    }
    
    public boolean isHasNormatSearchAttrs() {
        return hasNormatSearchAttrs;
    }
 
    public void setHasNormatSearchAttrs(boolean hasNormatSearchAttrs) {
        this.hasNormatSearchAttrs = hasNormatSearchAttrs;
    }
 
    public boolean isHasAdvanceSearchAttrs() {
        return hasAdvanceSearchAttrs;
    }
 
    public void setHasAdvanceSearchAttrs(boolean hasAdvanceSearchAttrs) {
        this.hasAdvanceSearchAttrs = hasAdvanceSearchAttrs;
    }
    
    
}