Source code: javax/ide/menu/spi/SectionVisitor.java
1 package javax.ide.menu.spi;
2
3 import java.util.logging.Level;
4
5 import javax.ide.extension.ElementContext;
6 import javax.ide.extension.ElementName;
7 import javax.ide.extension.ElementStartContext;
8 import javax.ide.extension.ElementVisitor;
9 import javax.ide.extension.ExtensionHook;
10
11 /**
12 * Visitor for menu sections.
13 */
14 final class SectionVisitor extends PositionableVisitor
15 {
16
17 final static ElementName SECTION = new ElementName(
18 ExtensionHook.MANIFEST_XMLNS, "section" );
19
20 private final static ElementName ITEM = new ElementName(
21 ExtensionHook.MANIFEST_XMLNS, "item" );
22
23
24 private ElementVisitor _menuVisitor = new MenuInSectionVisitor();
25 private ElementVisitor _itemVisitor = new ItemVisitor();
26
27 private final static String KEY_SECTION = "section";
28
29 protected void positionable(ElementStartContext context, String id, String before,
30 String after)
31 {
32 SectionContainer container = (SectionContainer)
33 context.getScopeData().get( MenuHook.KEY_SECTION_CONTAINER );
34
35 Section section = container.getSection( id );
36 if ( section == null )
37 {
38 section = new Section( id );
39 container.addSection( section );
40 }
41
42 if ( before != null )
43 {
44 section.setBefore( before );
45 }
46 else if ( after != null )
47 {
48 section.setAfter( after );
49 }
50
51
52 context.getScopeData().put( KEY_SECTION, section );
53
54 context.registerChildVisitor( MenuVisitor.MENU, _menuVisitor );
55 context.registerChildVisitor( ITEM, _itemVisitor );
56 }
57
58 private Section getSection( ElementContext context )
59 {
60 return (Section) context.getScopeData().get( KEY_SECTION );
61 }
62
63
64 private final class ItemVisitor extends PositionableVisitor
65 {
66 ItemVisitor()
67 {
68 super( "action-ref" ); // items use action-ref instead of id.
69 }
70
71 protected void positionable(ElementStartContext context, String id,
72 String before, String after)
73 {
74 Section parentSection = getSection( context );
75 Positionable pos = parentSection.getItem( id );
76
77 if ( pos == null )
78 {
79 pos = new Item( id );
80 parentSection.addItem( pos );
81 }
82 else
83 {
84 if ( pos instanceof Menu )
85 {
86 log( context, Level.SEVERE,
87 "'"+id+"' is already defined as a menu in section '"+parentSection.getID()+"'" );
88 return;
89 }
90 }
91
92 Item item = (Item) pos;
93
94 String radioGroupId = context.getAttributeValue( "radiogroup-id" );
95 if ( radioGroupId != null )
96 {
97 radioGroupId = radioGroupId.trim();
98 item.setRadioGroupID( radioGroupId );
99 }
100
101 if ( before != null ) item.setBefore( before );
102 else if ( after != null ) item.setAfter( after );
103 }
104 }
105
106 private final class MenuInSectionVisitor extends MenuVisitor
107 {
108 protected void positionable(ElementStartContext context, String id,
109 String before, String after)
110 {
111 Section parentSection = getSection( context );
112
113 Positionable pos = parentSection.getItem( id );
114
115 if ( pos == null )
116 {
117 pos = new Menu( id );
118 parentSection.addItem( pos );
119 }
120 else
121 {
122 if ( pos instanceof Item )
123 {
124 log( context, Level.SEVERE,
125 "'"+id+"' is already defined as an item in section '"+parentSection.getID()+"'" );
126 return;
127 }
128 }
129
130 final Menu menu = (Menu) pos;
131 processMenu( context, menu, before, after );
132
133 }
134 }
135
136 }