1 10 package mondrian.gui; 11 12 import java.awt.Color ; 13 import java.awt.Component ; 14 import java.awt.FlowLayout ; 15 import java.awt.Font ; 16 import java.awt.event.ItemEvent ; 17 import java.awt.event.ItemListener ; 18 import java.awt.event.MouseAdapter ; 19 import java.awt.event.MouseEvent ; 20 import java.io.File ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Vector ; 24 import javax.swing.ComboBoxModel ; 25 import javax.swing.DefaultComboBoxModel ; 26 import javax.swing.JComboBox ; 27 import javax.swing.JInternalFrame ; 28 import javax.swing.JLabel ; 29 import javax.swing.JMenuItem ; 30 import javax.swing.JOptionPane ; 31 import javax.swing.JPanel ; 32 import javax.swing.JPopupMenu ; 33 import javax.swing.SwingConstants ; 34 import javax.swing.UIManager ; 35 import javax.swing.text.DefaultEditorKit ; 36 import mondrian.olap.Connection; 37 import mondrian.olap.DriverManager; 38 import mondrian.olap.Query; 39 import mondrian.olap.Result; 40 41 46 public class QueryPanel extends javax.swing.JPanel { 47 Connection connection; 48 JMenuItem queryMenuItem; 49 int windowMenuIndex; 50 Map schemaWindowMap; 53 54 public QueryPanel() { 55 initComponents(); 56 } 57 58 public void setConnection(Connection c) { 59 connection = c; 60 } 61 62 public Connection getConnection() { 63 return connection; 64 } 65 66 68 72 public void setMenuItem(JMenuItem mi) { 73 this.queryMenuItem = mi; 74 } 75 76 public void setSchemaWindowMap(Map swindowMap) { 77 this.schemaWindowMap = swindowMap; setCatalogs(); 79 } 80 81 private void setCatalogs() { 82 Vector v = new Vector (); 83 Iterator it = schemaWindowMap.values().iterator(); while (it.hasNext()) { 85 JMenuItem elem = (JMenuItem ) it.next(); 86 v.add(elem.getText()); 87 } 88 ComboBoxModel cCatalogs = new DefaultComboBoxModel (v); 89 schemaList.setModel(cCatalogs) ; 90 } 91 public void setWindowMenuIndex(int i) { 92 this.windowMenuIndex = i; 93 } 94 public void initConnection(String smenutext) { 95 schemaList.setSelectedItem(smenutext); 96 connectButtonActionPerformed(null); 97 } 98 100 104 109 private void initComponents() { 110 jScrollPane3 = new javax.swing.JScrollPane (); 111 executeButton = new javax.swing.JButton (); 112 jSplitPane1 = new javax.swing.JSplitPane (); 113 jScrollPane1 = new javax.swing.JScrollPane (); 114 queryTextPane = new javax.swing.JTextPane (); 115 jScrollPane2 = new javax.swing.JScrollPane (); 116 resultTextPane = new javax.swing.JTextPane (); 117 connectButton = new javax.swing.JButton (); 118 jPopupMenu = new JPopupMenu (); 119 jPopupMenu.add(new DefaultEditorKit.CutAction ()); 120 jPopupMenu.add(new DefaultEditorKit.CopyAction ()); 121 jPopupMenu.add(new DefaultEditorKit.PasteAction ()); 122 123 schemaScrollPane1 = new javax.swing.JScrollPane (); 124 schemaLabel = new javax.swing.JLabel (); 125 schemaList = new JComboBox ( new String [] {"Join", "Table"} ); 126 schemaPanel = new JPanel (); 128 schemaPanel.setLayout(new FlowLayout (FlowLayout.LEFT, 10, 0)); 130 131 setLayout(new java.awt.BorderLayout ()); 132 133 schemaLabel.setFont(new Font ("Dialog", 1, 12)); 134 schemaLabel.setForeground((Color ) UIManager.getDefaults().get("CheckBoxMenuItem.acceleratorForeground")); 135 schemaLabel.setHorizontalAlignment(SwingConstants.CENTER); 136 schemaLabel.setText(" Schema "); 137 139 schemaList.setBackground(Color.white); 140 final JPanel qpanel = this; 141 schemaList.addItemListener(new ItemListener () { 142 public void itemStateChanged(ItemEvent e) { 143 144 145 } 146 }); 147 connectButton.setText("Connect"); 148 connectButton.addActionListener(new java.awt.event.ActionListener () { 149 public void actionPerformed(java.awt.event.ActionEvent evt) { 150 connectButtonActionPerformed(evt); 151 } 152 }); 153 154 schemaPanel.add(schemaLabel); schemaPanel.add(schemaList); 156 schemaPanel.add(connectButton); 157 158 add(schemaPanel, java.awt.BorderLayout.NORTH); 159 160 executeButton.setText("Execute"); 161 executeButton.addActionListener(new java.awt.event.ActionListener () { 162 public void actionPerformed(java.awt.event.ActionEvent evt) { 163 executeButtonActionPerformed(evt); 164 } 165 }); 166 167 add(executeButton, java.awt.BorderLayout.SOUTH); 168 169 jSplitPane1.setDividerLocation(100); 170 jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); 171 queryTextPane.setFont(new java.awt.Font ("Courier New", 0, 12)); 172 queryTextPane.setText(""); 173 queryTextPane.addMouseListener(new MouseAdapter () { 174 public void mouseReleased(MouseEvent e) { 175 if (e.isPopupTrigger()) { 176 int x = e.getX(); 177 int y = e.getY(); 178 179 jPopupMenu.show(queryTextPane, x, y); 180 181 } 182 } 183 }); 184 185 jScrollPane1.setViewportView(queryTextPane); 186 187 jSplitPane1.setTopComponent(jScrollPane1); 188 189 resultTextPane.setEditable(false); 190 resultTextPane.setFont(new java.awt.Font ("Courier New", 0, 12)); 191 jScrollPane2.setViewportView(resultTextPane); 192 193 jSplitPane1.setBottomComponent(jScrollPane2); 194 195 add(jSplitPane1, java.awt.BorderLayout.CENTER); 196 197 } 198 199 private void executeButtonActionPerformed(java.awt.event.ActionEvent evt) { try { 202 if (connection == null) { 203 JOptionPane.showMessageDialog(this, "No Mondrian connection. Select a Schema to connect.", "Alert" , JOptionPane.WARNING_MESSAGE); 204 return; 205 } 206 Query q = connection.parseQuery(queryTextPane.getText()); 207 Result r = connection.execute(q); 208 209 java.io.StringWriter sw = new java.io.StringWriter (); 211 java.io.PrintWriter pw = new java.io.PrintWriter (sw); 212 213 r.print(pw); 214 215 resultTextPane.setText(sw.getBuffer().toString()); 216 } catch (Exception ex) { 217 resultTextPane.setText(ex.getMessage()); 218 } 219 220 } 222 226 private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) { 227 File sfile = null; 228 try { 229 JInternalFrame sf = null; 230 231 String sfname = (String ) schemaList.getSelectedItem(); 232 Iterator it = schemaWindowMap.entrySet().iterator(); 233 while (it.hasNext()) { 234 Map.Entry elem = (Map.Entry ) it.next(); 235 if (((JMenuItem ) elem.getValue()).getText().equals(sfname)) { 236 sf = (JInternalFrame ) elem.getKey(); 237 break; 238 } 239 } 240 241 if (sf == null) { 242 JOptionPane.showMessageDialog(this, "Schema file is not open.", "Error" , JOptionPane.ERROR_MESSAGE); 244 return; 245 } 246 247 SchemaExplorer se = (SchemaExplorer) sf.getContentPane().getComponent(0); 249 if (se.isNewFile()) { 250 JOptionPane.showMessageDialog(this, "You must first save the Schema to open a Mondrian comnnection.", "Alert" , JOptionPane.WARNING_MESSAGE); 251 sf.setSelected(true); 252 return; 253 } 254 sfile = se.getSchemaFile(); 255 256 String connectString = "Provider=mondrian;" + "Jdbc=" + se.getJdbcConnectionUrl() + ";" + "Catalog=" + se.getSchemaFile().toURL().toString(); 257 Connection con = DriverManager.getConnection(connectString, null, false); 258 if (con != null) { 259 connection = con; 260 queryMenuItem.setText(windowMenuIndex + " MDX - "+se.getSchemaFile().getName()); 261 Component o = this; 262 while (o != null) { 263 if (o.getClass() == JInternalFrame .class) { 265 ((JInternalFrame ) o).setTitle("MDX Query - connected to "+se.getSchemaFile().getName()); 266 break; 267 } 268 o = o.getParent(); 269 } 270 JOptionPane.showMessageDialog(this, "Mondrian connection Successful.", "Information" , JOptionPane.INFORMATION_MESSAGE); 271 } else { 272 JOptionPane.showMessageDialog(this, "Mondrian connection could not be done for - "+se.getSchemaFile().getName(), "Error" , JOptionPane.ERROR_MESSAGE); 273 } 274 } catch (Exception ex) { 275 System.out.println("Exception: "+ex.getMessage()); 276 JOptionPane.showMessageDialog(this, "Mondrian connection could not be done for "+(sfile == null?"selected Schema.":sfile.getName()), "Error" , JOptionPane.ERROR_MESSAGE); 277 } 278 } 279 private javax.swing.JScrollPane jScrollPane3; 281 private javax.swing.JScrollPane jScrollPane2; 282 private javax.swing.JTextPane resultTextPane; 283 private javax.swing.JScrollPane jScrollPane1; 284 private javax.swing.JTextPane queryTextPane; 285 private javax.swing.JSplitPane jSplitPane1; 286 private javax.swing.JButton executeButton; 287 private javax.swing.JComboBox schemaList; 288 private JLabel schemaLabel; 289 private JPanel schemaPanel; 290 private javax.swing.JScrollPane schemaScrollPane1; 291 private javax.swing.JButton connectButton; 292 private JPopupMenu jPopupMenu; 293 294 296 } 297 298 | Popular Tags |