RmiClient  1.0
 Alle Klassen Namensbereiche Dateien Funktionen Variablen
AbstractDialog.java
gehe zur Dokumentation dieser Datei
1 package view;
2 
3 import java.awt.Color;
4 import java.awt.FlowLayout;
5 import java.awt.HeadlessException;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.ItemEvent;
9 import java.awt.event.KeyEvent;
10 import java.lang.reflect.Constructor;
11 import java.lang.reflect.InvocationTargetException;
12 import java.lang.reflect.Method;
13 import java.rmi.RemoteException;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Observable;
17 import java.util.Observer;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20 
21 import javax.persistence.Column;
22 import javax.persistence.Entity;
23 import javax.persistence.metamodel.Attribute;
24 import javax.persistence.metamodel.ManagedType;
25 import javax.swing.JButton;
26 import javax.swing.JComboBox;
27 import javax.swing.JComponent;
28 import javax.swing.JDialog;
29 import javax.swing.JFrame;
30 import javax.swing.JOptionPane;
31 import javax.swing.JPanel;
32 import javax.swing.JTextField;
33 import javax.swing.KeyStroke;
34 import javax.swing.SwingUtilities;
35 
36 import main.CustomItemListener;
37 import main.ProjektStatus;
38 import main.ResultContent;
39 import main.UserRole;
40 
41 import controller.Controller;
42 import databaseModel.Ansprechpartner;
43 import databaseModel.Projekt;
44 import databaseModel.Student;
45 import databaseModel.Unternehmen;
46 
53 public abstract class AbstractDialog extends JDialog implements Observer {
54  private static final long serialVersionUID = 1L;
55 
59  protected JDialog activeDialog = null;
60 
64  protected boolean newMode = true;
65 
69  private JButton okButton = null;
70  private JButton closeButton = null;
71  protected JPanel buttonPanel = null;
72 
76  protected ArrayList<JComponent> mandatoryComponentList = new ArrayList<JComponent>();
77 
81  protected Object changeObject = null;
82 
86  protected List<Object> projekteListe = null;
87  protected List<Object> unternehmenListe = null;
88  protected List<Object> ansprechpartnerListe = null;
89  protected List<Object> studentenListe = null;
90 
94  protected final Controller controller;
95  protected final UserRole roleType;
96 
100  protected final int defaultInputFieldLength = 30;
101 
105  protected final int defaultVisibleFieldLength = 30;
106 
110  protected final int defaultTextAreaRows = 10;
111 
115  protected final int defaultTextAreaColumns = 30;
116 
120  protected final Color defaultColorField = null;
121 
125  protected final Color defaultColorMandatoryField = Color.orange;
126 
130  protected final Color defaultColorMissingMandatoryField = Color.red;
131 
135  protected final int defaultConfirmMnemonic = KeyEvent.VK_B;
136 
140  protected final int defaultCancelMnemonic = KeyEvent.VK_C;
141 
145  protected ArrayList<String> errorList = new ArrayList<String>();
146 
155  public AbstractDialog(JFrame owner, Controller controller, String label,
156  UserRole roleType) {
157  super(owner, label);
158  this.controller = controller;
159  this.roleType = roleType;
160  // activeDialog kann nicht im Konstruktor gesetzt werden
161 
165  buttonPanel = new JPanel();
166  buttonPanel.setLayout(new FlowLayout());
167  okButton = new JButton("Übernehmen");
168  okButton.setMnemonic(defaultConfirmMnemonic);
169  okButton.addActionListener(new ActionListener() {
170  @Override
171  public void actionPerformed(ActionEvent arg0) {
172  SwingUtilities.invokeLater(new Runnable() {
173  public void run() {
178 
179  saveData();
180  }
181  });
182  }
183  });
184 
185  closeButton = new JButton("Abbrechen");
186  closeButton.setMnemonic(defaultCancelMnemonic);
187  closeButton.addActionListener(new ActionListener() {
188  @Override
189  public void actionPerformed(ActionEvent arg0) {
190  SwingUtilities.invokeLater(new Runnable() {
191  public void run() {
192  closeForm();
193  }
194  });
195  }
196  });
197  buttonPanel.add(okButton);
198  buttonPanel.add(closeButton);
199 
204  getRootPane().registerKeyboardAction(new ActionListener() {
205  public void actionPerformed(ActionEvent e) {
206  closeForm();
207  }
208  }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
209  JComponent.WHEN_IN_FOCUSED_WINDOW);
210  }
211 
216  protected void initializeFieldsWithColors() {
217  for (JComponent c : mandatoryComponentList) {
218  c.setBackground(defaultColorMandatoryField);
219  }
220  }
221 
226  protected void saveData() {
227  }
228 
239  protected int getColumnLength(Class classType, String columnName) {
240  int columnLength = defaultInputFieldLength;
241 
242  try {
243  Column column = classType.getDeclaredField(columnName)
244  .getAnnotation(Column.class);
245  if (column != null) {
246  columnLength = column.length();
247  }
248  } catch (NoSuchFieldException e) {
249  // TODO Auto-generated catch block
250  e.printStackTrace();
251  } catch (SecurityException e) {
252  // TODO Auto-generated catch block
253  e.printStackTrace();
254  }
255 
256  return columnLength;
257  }
258 
268  protected void preSelection(Class classType) {
269  try {
270  List<Object> objects = Controller.rmiServer.getData(classType);
271  update(null, new ResultContent(classType, objects));
272  } catch (RemoteException e2) {
273  // TODO Auto-generated catch block
274  e2.printStackTrace();
275  }
276  }
277 
283  public void initialize(final String label, final Object projekt) {
284  }
285 
293  @Override
294  public void update(Observable arg0, final Object arg1) {
295  if (arg1 != null) {
296  Class listType = ((ResultContent) arg1).getListType();
297  List<Object> listContent = ((ResultContent) arg1).getListContent();
298 
299  if (changeObject != null) {
300  if (listType == changeObject.getClass()) {
302  listContent);
303  }
304  }
305  }
306  }
307 
314  protected void checkValidEmail(JTextField emailField) {
315  if (!emailField.getText().trim().equals("")) {
320  Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
321  Matcher m = p.matcher(emailField.getText().trim());
322 
323  if (!m.matches()) {
324  errorList
325  .add("Es wurde keine gültige E-Mail-Adresse eingegeben!");
326  emailField.setBackground(defaultColorMissingMandatoryField);
327  }
328  }
329  }
330 
337  protected void checkValidOnlyNumbers(JTextField matField) {
338  if (!matField.getText().trim().equals("")) {
344  Pattern p = Pattern.compile("\\p{Digit}+");
345  Matcher m = p.matcher(matField.getText().trim());
346 
347  if (!m.matches()) {
348  errorList
349  .add("Das Matrikelnummernfeld darf nur Ziffern enthalten!");
350  matField.setBackground(defaultColorMissingMandatoryField);
351  }
352  }
353  }
354 
374  protected void updateGUIData(Class listType, List<Object> listContent,
375  boolean newMode, JComboBox comboBox, Object oldSelectedData,
376  boolean specialCase) {
385  ((CustomItemListener) comboBox.getItemListeners()[0]).setActive(false);
386 
390  Integer comboboxIndex = 0;
391  boolean found = false;
392  comboBox.setSelectedIndex(-1);
393  comboBox.removeAllItems();
394 
398  if (specialCase) {
399  comboBox.addItem("");
400  comboboxIndex = 1;
401  }
402  Integer foundIndex = comboboxIndex;
403 
407  for (Object a : listContent) {
408  try {
409  try {
413  Class classA = a.getClass();
414  Method methodClassAGetName = classA.getMethod("getName");
415 
416  String resultClassAGetName = (String) methodClassAGetName
417  .invoke(a);
418  comboBox.addItem(resultClassAGetName);
419 
424  if (!newMode) {
425  if (oldSelectedData != null) {
426  Method methodClassAGetId = classA
427  .getMethod("getId");
428 
429  Class classB = oldSelectedData.getClass();
430  Method methodClassBGetId = classB
431  .getMethod("getId");
432 
433  int resultClassAGetId = (Integer) methodClassAGetId
434  .invoke(a);
435  int resultClassBGetId = (Integer) methodClassBGetId
436  .invoke(oldSelectedData);
437 
438  if (resultClassAGetId == resultClassBGetId) {
439  foundIndex = comboboxIndex;
440  found = true;
441  }
442  }
443  }
444  } catch (IllegalAccessException e) {
445  // TODO Auto-generated catch block
446  e.printStackTrace();
447  } catch (IllegalArgumentException e) {
448  // TODO Auto-generated catch block
449  e.printStackTrace();
450  } catch (InvocationTargetException e) {
451  // TODO Auto-generated catch block
452  e.printStackTrace();
453  }
454 
455  } catch (NoSuchMethodException e) {
456  // TODO Auto-generated catch block
457  e.printStackTrace();
458  } catch (SecurityException e) {
459  // TODO Auto-generated catch block
460  e.printStackTrace();
461  }
462  comboboxIndex++;
463  }
464 
468  if (newMode) {
469  if (listContent.size() > 0) {
470  comboBox.setSelectedIndex(0);
471  }
472  } else {
473  if (found) {
477  comboBox.setSelectedIndex(foundIndex);
478  }
479  }
480 
484  ((CustomItemListener) comboBox.getItemListeners()[0]).setActive(true);
485 
493  /*
494  * Object selectedItem = comboBox.getSelectedItem();
495  * ((CustomItemListener) comboBox.getItemListeners()[0])
496  * .itemStateChanged(new ItemEvent(comboBox,
497  * ItemEvent.ITEM_STATE_CHANGED, selectedItem, ItemEvent.SELECTED));
498  */
499 
504  if (listType == Ansprechpartner.class) {
505  ansprechpartnerListe = listContent;
506  } else if (listType == Projekt.class) {
507  projekteListe = listContent;
508  } else if (listType == Unternehmen.class) {
509  unternehmenListe = listContent;
510  } else if (listType == Student.class) {
511  studentenListe = listContent;
512  }
513  }
514 
525  protected void checkExistingRecord(Class classType, String checkingField,
526  Object checkingClauseComponent) {
527  boolean OK = true;
528  String checkingClause = "";
529 
530  if (checkingClauseComponent.getClass() == JTextField.class) {
531  checkingClause = ((JTextField) checkingClauseComponent).getText()
532  .trim();
533  }
534 
535  if (!checkingClause.equals("")) {
536  try {
537  List<Object> existingRecords = Controller.rmiServer
538  .getDataWithClause(classType, checkingField,
539  checkingClause);
540 
541  if (newMode) {
542  OK = !(existingRecords.size() > 0);
543  } else {
548  for (Object a : existingRecords) {
549  Class classA = a.getClass();
550  Class classB = changeObject.getClass();
551  try {
552  Method methodClassA = classA.getMethod("getId");
553  Method methodClassB = classB.getMethod("getId");
554 
555  try {
556  int resultClassA = (Integer) methodClassA
557  .invoke(a);
558  int resultClassB = (Integer) methodClassB
559  .invoke(changeObject);
560 
561  if (resultClassA != resultClassB) {
562  OK = false;
563  break;
564  }
565  } catch (IllegalAccessException e) {
566  // TODO Auto-generated catch block
567  e.printStackTrace();
568  } catch (IllegalArgumentException e) {
569  // TODO Auto-generated catch block
570  e.printStackTrace();
571  } catch (InvocationTargetException e) {
572  // TODO Auto-generated catch block
573  e.printStackTrace();
574  }
575 
576  } catch (NoSuchMethodException e) {
577  // TODO Auto-generated catch block
578  e.printStackTrace();
579  } catch (SecurityException e) {
580  // TODO Auto-generated catch block
581  e.printStackTrace();
582  }
583  }
584  }
585  } catch (RemoteException e) {
586  // TODO Auto-generated catch block
587  e.printStackTrace();
588  }
589 
590  if (!OK) {
591  errorList.add("Dieser Datensatz ist bereits vorhanden!");
592  }
593  }
594 
595  if (!OK) {
596  if (checkingClauseComponent.getClass() == JTextField.class) {
597  ((JTextField) checkingClauseComponent)
598  .setBackground(defaultColorMissingMandatoryField);
599  }
600  }
601  }
602 
621  protected void closeFormOnDeleteCascading(Class classType,
622  List<Object> listContent) {
623  if (!newMode && changeObject != null && activeDialog.isVisible()) {
624  boolean existingFurthermore = false;
625 
626  for (Object a : listContent) {
627  Class classA = a.getClass();
628  Class classB = changeObject.getClass();
629  try {
630  Method methodClassA = classA.getMethod("getId");
631  Method methodClassB = classB.getMethod("getId");
632 
633  try {
634  int resultClassA = (Integer) methodClassA.invoke(a);
635  int resultClassB = (Integer) methodClassB
636  .invoke(changeObject);
637 
638  if (resultClassA == resultClassB) {
639  existingFurthermore = true;
640  break;
641  }
642  } catch (IllegalAccessException e) {
643  // TODO Auto-generated catch block
644  e.printStackTrace();
645  } catch (IllegalArgumentException e) {
646  // TODO Auto-generated catch block
647  e.printStackTrace();
648  } catch (InvocationTargetException e) {
649  // TODO Auto-generated catch block
650  e.printStackTrace();
651  }
652 
653  } catch (NoSuchMethodException e) {
654  // TODO Auto-generated catch block
655  e.printStackTrace();
656  } catch (SecurityException e) {
657  // TODO Auto-generated catch block
658  e.printStackTrace();
659  }
660  }
661 
662  if (!existingFurthermore) {
663  errorList
664  .add("Der aktuell bearbeitete Datensatz wurde direkt oder einer seiner referenzierten Datensätze gelöscht, wodurch er automatisch mitgelöscht wurde!");
665  errorList
666  .add("Stichwort \"kaskadierendes Löschen von Detaildatensätzen!\"!");
667  errorList
668  .add("Das aktive Fenster wird automatisch geschlossen!");
669 
670  showErrors();
671  closeForm();
672  }
673  }
674  }
675 
684  private String getErrorString() {
685  String result = "";
686  for (String s : errorList) {
687  result += s;
688  result += System.getProperty("line.separator");
689  }
690  return result;
691  }
692 
696  protected void showErrors() {
697  if (errorList.size() > 0) {
698  JOptionPane.showMessageDialog(null, getErrorString(), "Fehler",
699  JOptionPane.ERROR_MESSAGE);
700 
704  errorList.clear();
705  }
706  }
707 
711  protected void closeForm() {
715  newMode = true;
716  dispose();
717  }
718 }