View Javadoc

1   package com.explosion.datastream.exql.gui.table;
2   
3   import java.awt.event.MouseAdapter;
4   import java.awt.event.MouseEvent;
5   
6   import javax.swing.JOptionPane;
7   import javax.swing.table.JTableHeader;
8   import javax.swing.table.TableColumnModel;
9   
10  import org.apache.log4j.LogManager;
11  import org.apache.log4j.Logger;
12  
13  import com.explosion.datastream.exql.gui.dbbrowser.DBDataViewer;
14  import com.explosion.expf.Application;
15  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntity;
16  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntityBuffer;
17  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntityColumn;
18  
19  
20  /* =============================================================================
21   *       
22   *     Copyright 2004 Stephen Cowx
23   *
24   *     Licensed under the Apache License, Version 2.0 (the "License");
25   *     you may not use this file except in compliance with the License.
26   *     You may obtain a copy of the License at
27   *
28   *     http://www.apache.org/licenses/LICENSE-2.0
29   *
30   *     Unless required by applicable law or agreed to in writing, software
31   *     distributed under the License is distributed on an "AS IS" BASIS,
32   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33   *     See the License for the specific language governing permissions and
34   *     limitations under the License.
35   * 
36   * =============================================================================
37   */
38  
39  /***
40   * @author Stephen Cowx
41   * Created on Apr 20, 2004
42   */
43  public class Sort_MouseListener extends MouseAdapter
44  {
45      private static Logger log = LogManager.getLogger(Sort_MouseListener.class);
46      DBEntityBuffer buffer;
47      DBDataViewer view;
48      DBEntity entity = null;
49      private int count = 0;
50      
51      public Sort_MouseListener(DBDataViewer view)
52      {
53          log.debug("Sort_MouseListener");
54          this.view = view;
55      }
56      
57      
58      
59      /***
60       * Handle events generated within the table header.  Cycles the sorting status
61       * in the DBEntityBuffer
62       * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
63       * @param e
64       */
65      public void mouseClicked(MouseEvent e)
66      {
67          count++;
68          if (e.getClickCount() > 1)
69          {
70              JTableHeader h = (JTableHeader) e.getSource();
71              TableColumnModel columnModel = h.getColumnModel();
72              int viewColumn = columnModel.getColumnIndexAtX(e.getX());
73              //int column = h.getTable().columnModel.getColumn(viewColumn).getModelIndex();
74              int column = viewColumn;//h.getTable().convertColumnIndexToModel(viewColumn);
75              
76              String name = columnModel.getColumn(column).getIdentifier().toString();
77              if (column != -1)
78              {
79                  DBEntityColumn col = entity.getColumn(name);
80                  if (col != null && !col.isSortable())
81                  {
82                      JOptionPane.showMessageDialog(Application.getApplicationFrame(), "Sorting columns of type "+col.getTypeName()+" is not supported.");
83                      return;
84                  }
85                  
86                  int status = buffer.getSortDirection(name);
87                  if (e.isControlDown())
88                  {
89                      buffer.removeFromSort(name);
90                  }
91                  
92                 // Cycle the sorting states through {NOT_SORTED, ASCENDING,
93                  // DESCENDING} or
94                  // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is
95                  // pressed.
96                  status = status + (e.isShiftDown() ? -1 : 1);
97                  status = (status + 1) % 3 - 1; // signed mod returning {-1, 0, 1}
98                  buffer.setSortingStatus(name, status);
99                  view.update();
100                 Application.getApplicationFrame().repaint();
101             }
102         }
103     }
104     /***
105      * @return Returns the entity.
106      */
107     public DBEntity getEntity()
108     {
109         return entity;
110     }
111     /***
112      * @param entity The entity to set.
113      */
114     public void setEntity(DBEntity entity)
115     {
116         this.entity = entity;
117         this.buffer = entity.getDBEntityBuffer();
118     }
119 }