1 package org.telscenter.pas.authortool.cards;
2
3 import java.awt.CardLayout;
4 import java.awt.Component;
5 import java.lang.reflect.Constructor;
6 import java.lang.reflect.InvocationTargetException;
7
8 import javax.swing.JPanel;
9
10 import org.telscenter.pas.authortool.context.CurnitAuthoringContext;
11
12
13 public class CardProvider {
14
15 private JPanel cardPanel;
16
17 private CurnitAuthoringContext curnitAuthoringContext;
18
19 private CardLayout cl;
20
21 private String currentCardName;
22
23 public CardProvider(final CurnitAuthoringContext curnitAuthoringContext) {
24 this.curnitAuthoringContext = curnitAuthoringContext;
25 final CardLayout cardLayout = new CardLayout();
26 cardPanel = new JPanel(cardLayout);
27
28 final Class[] cardClasses = curnitAuthoringContext.getCardClasses();
29
30 for (final Class element : cardClasses)
31 try {
32 addCard(element);
33 } catch (final Exception e) {
34
35 e.printStackTrace();
36 }
37 }
38
39 public void addCard(final Class cardClass) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
40 final Class[] parameters = new Class[] {CurnitAuthoringContext.class};
41 final Constructor constr = cardClass.getConstructor(parameters);
42 final Object[] initargs = new Object[] {curnitAuthoringContext};
43 final AbstractCurnitAuthoringCard card = (AbstractCurnitAuthoringCard) constr.newInstance(initargs);
44
45 if( card.getMenu() != null ) {
46 curnitAuthoringContext.getMenuProvider().addNewMenu(card.getMenu());
47 }
48 card.setName(cardClass.getName());
49 cardPanel.add(card, cardClass.getName());
50 }
51
52 public JPanel getCardPanel() {
53 return cardPanel;
54 }
55
56 public void removeCardPanel(Class cardClass) {
57 cardPanel.remove(getCard(cardClass.getName()));
58 }
59
60 public void showCard(final Class cardClass) {
61 cl = (CardLayout) cardPanel.getLayout();
62 cl.show(cardPanel, cardClass.getName());
63
64
65 AbstractCurnitAuthoringCard card = (AbstractCurnitAuthoringCard) this.getCard(cardClass.getName());
66 try {
67 curnitAuthoringContext.getMenuProvider().addNewMenu(card.getMenu());
68 } catch (SecurityException e) {
69 e.printStackTrace();
70 } catch (IllegalArgumentException e) {
71 e.printStackTrace();
72 } catch (NoSuchMethodException e) {
73 e.printStackTrace();
74 } catch (InstantiationException e) {
75 e.printStackTrace();
76 } catch (IllegalAccessException e) {
77 e.printStackTrace();
78 } catch (InvocationTargetException e) {
79 e.printStackTrace();
80 }
81 }
82
83
84 public Component getCard(String cardName) {
85 int nChildren = cardPanel.getComponentCount();
86 for (int i = 0; i < nChildren; i++) {
87 Component child = cardPanel.getComponent(i);
88 if (child.getName().equals(cardName)) {
89 return child;
90 }
91 }
92
93 return null;
94 }
95
96
97 }