private void sortAddToTree(Object sid) { // // sort session elements // List<TestSession> caseSessions = parentCase.getSessions(); // List<Integer> sortedids = new ArrayList<Integer>(); // for (TestSession s : caseSessions){ // sortedids.add(s.getId()); // // remove tree items // tree.removeItem(s.getId()); // } // Collections.sort(sortedids); // Collections.reverse(sortedids); // System.out.println("SORTED ID's: " + sortedids); List<Object> sortedids = new ArrayList<Object>(); for (Object child : tree.getChildren(parentcase.getId())) { sortedids.add(child); } for (Object s : sortedids) { // remove tree items tree.removeItem(s); } // add new item and then re-add old items tree.addItem(sid); tree.setParent(sid, parentcase.getId()); tree.setChildrenAllowed(sid, false); tree.setItemCaption(sid, sessions.getItem(sid).getEntity().getTitle()); // newsession.getTitle() tree.expandItem(parentcase.getId()); tree.select(sid); for (Object id : sortedids) { // testcase.getSessions() matchingsessions Object sessionid = sessions.getItem(id).getEntity().getId(); tree.addItem(sessionid); tree.setItemCaption(sessionid, sessions.getItem(id).getEntity().getTitle()); tree.setParent(sessionid, parentcase.getId()); tree.setChildrenAllowed(sessionid, false); } }
public void init(final Window main) { VerticalLayout mainLayout = new VerticalLayout(); main.setContent(mainLayout); mainLayout.setSizeFull(); HorizontalLayout titlebar = new HorizontalLayout(); titlebar.addStyleName("titlebar"); titlebar.setWidth("100%"); Label title = new Label("Book of Vaadin Examples"); title.addStyleName("title"); titlebar.addComponent(title); titlebar.setComponentAlignment(title, Alignment.MIDDLE_RIGHT); Embedded logo = new Embedded(null, new ThemeResource("img/vaadin-logo.png")); titlebar.addComponent(logo); titlebar.setComponentAlignment(logo, Alignment.MIDDLE_RIGHT); main.addComponent(titlebar); HorizontalLayout hor = new HorizontalLayout(); hor.setSizeFull(); main.addComponent(hor); mainLayout.setExpandRatio(hor, 1.0f); final Panel menupanel = new Panel("Examples"); menupanel.addStyleName("menupanel"); menupanel.setWidth(null); menupanel.setHeight("100%"); menupanel.getContent().setWidth(null); // menupanel.getContent().setHeight("100%"); hor.addComponent(menupanel); final Tree menu = new Tree(); menu.setWidth(null); // menu.setHeight("100%"); menu.setImmediate(true); menupanel.addComponent(menu); final Panel viewpanel = new Panel("Selected Example"); viewpanel.addStyleName("viewpanel"); viewpanel.setSizeFull(); VerticalLayout viewlayout = new VerticalLayout(); viewlayout.addStyleName("viewlayout"); viewlayout.setSpacing(true); viewlayout.setMargin(true); viewpanel.setContent(viewlayout); hor.addComponent(viewpanel); hor.setExpandRatio(viewpanel, 1.0f); WebApplicationContext ctx = (WebApplicationContext) getContext(); BookExampleLibrary library = BookExampleLibrary.getInstance(ctx.getBaseDirectory()); AbstractExampleItem[] examples = library.getAllExamples(); // Collect redirects here final HashMap<String, String> redirects = new HashMap<String, String>(); // Collect examples here final HashMap<String, CaptionedExampleItem> exampleitems = new HashMap<String, CaptionedExampleItem>(); // Build the menu and collect redirections for (int i = 0; i < examples.length; i++) if (examples[i] instanceof BookExample || examples[i] instanceof ExampleCtgr) { CaptionedExampleItem example = (CaptionedExampleItem) examples[i]; exampleitems.put(example.getExampleId(), example); String itemid = example.getExampleId(); menu.addItem(itemid); menu.setItemCaption(itemid, example.getShortName()); if (examples[i].getParentId() != null) menu.setParent(itemid, examples[i].getParentId()); } else if (examples[i] instanceof RedirctItem) { RedirctItem redirect = (RedirctItem) examples[i]; redirects.put(redirect.getExampleId(), redirect.redirectid); } // Expand the menu for (int i = 0; i < examples.length; i++) { if (examples[i].getParentId() == null) menu.expandItemsRecursively(examples[i].getExampleId()); if (examples[i].isCollapsed()) menu.collapseItem(examples[i].getExampleId()); if (menu.getChildren(examples[i].getExampleId()) == null) menu.setChildrenAllowed(examples[i].getExampleId(), false); } // Set selected example as given in the URI fragment final UriFragmentUtility urifu = new UriFragmentUtility(); urifu.addListener( new FragmentChangedListener() { private static final long serialVersionUID = -6588416218607827834L; public void fragmentChanged(FragmentChangedEvent source) { String fragment = source.getUriFragmentUtility().getFragment(); if (fragment != null) { // Handle redirection while (redirects.containsKey(fragment)) fragment = redirects.get(fragment); menu.setValue(fragment); // Open the tree nodes leading to the example for (Object parent = menu.getParent(fragment); parent != null; parent = menu.getParent(parent)) menu.expandItem(parent); } } }); mainLayout.addComponent(urifu); // Handle menu selection menu.addListener( new Property.ValueChangeListener() { private static final long serialVersionUID = 8236533959795019956L; public void valueChange(ValueChangeEvent event) { viewpanel.removeAllComponents(); String selection = (String) event.getProperty().getValue(); // Find the example CaptionedExampleItem exampleItem = exampleitems.get(selection); if (selection != null && exampleItem == null) main.showNotification("Invalid item " + selection); else if (exampleItem != null) { if (exampleItem.getClass().isAssignableFrom(ExampleCtgr.class)) { if (menu.hasChildren(exampleItem.getExampleId())) { menu.select((String) menu.getChildren(exampleItem.getExampleId()).toArray()[0]); } } else { // A leaf BookExample example = (BookExample) exampleItem; // Load unless already loaded WebApplicationContext ctx = (WebApplicationContext) getContext(); example.loadExample(ctx.getBaseDirectory()); if (example.getDescription() != null) { Label descLabel = new Label(example.getDescription(), Label.CONTENT_XHTML); descLabel.addStyleName("example-description"); viewpanel.addComponent(descLabel); } // The actual example component viewpanel.addComponent(example.createInstance()); // Java sources on the left, CSS on the right HorizontalLayout horizontalOrder = new HorizontalLayout(); horizontalOrder.addStyleName("sourcecontainer"); horizontalOrder.setSpacing(true); horizontalOrder.setMargin(true); Panel bookRefs = null; Panel forumLinks = null; Panel kbRefs = null; List<SourceFragment> fragments = example.getSourceFragments(); if (fragments != null) { // Java Sources are laid out vertically VerticalLayout verticalListings = new VerticalLayout(); verticalListings.setSizeUndefined(); verticalListings.setSpacing(true); horizontalOrder.addComponent(verticalListings); // Find the widest source fragment int widestIndex = 0; int widestWidth = 0; for (int fragmentNum = 0; fragmentNum < fragments.size(); fragmentNum++) if (fragments.get(fragmentNum).getSrcWidth() > widestWidth) { widestIndex = fragmentNum; widestWidth = fragments.get(fragmentNum).getSrcWidth(); } System.out.println("Widest listing: " + widestIndex + " which is " + widestWidth); for (int fragmentNum = 0; fragmentNum < fragments.size(); fragmentNum++) { SourceFragment fragment = fragments.get(fragmentNum); // Have caption only in the beginning of the listings String listingCaption = fragmentNum == 0 ? "Source Code" : ""; String srcurl = "http://dev.vaadin.com/browser/doc/book-examples/trunk/src" + fragment.getSrcName(); SourceListing listing = new SourceListing(listingCaption, srcurl, fragment); verticalListings.addComponent(listing); // Use the width of the widest listing for all listings if (fragmentNum == widestIndex) listing.setWidth(Sizeable.SIZE_UNDEFINED, 0); else listing.setWidth("100%"); if (!fragment.getBookRefs().isEmpty()) { bookRefs = new Panel("Book References"); bookRefs.setSizeUndefined(); for (Iterator<String> iter = fragment.getBookRefs().iterator(); iter.hasNext(); ) { String ref = iter.next(); int hashPos = ref.indexOf('#'); String refFragment = ""; if (hashPos != -1) { refFragment = "#" + ref.replace('#', '.'); ref = ref.substring(0, hashPos); } String bookUrl = "http://vaadin.com/book/-/page/" + ref + ".html" + refFragment; Link link = new Link(bookUrl, new ExternalResource(bookUrl)); link.setTargetName("_new"); bookRefs.addComponent(link); } } if (!fragment.getForumLinks().isEmpty()) { forumLinks = new Panel("Forum Messages"); forumLinks.setSizeUndefined(); for (Iterator<String> iter = fragment.getForumLinks().iterator(); iter.hasNext(); ) { String url = iter.next(); Link link = new Link(url, new ExternalResource(url)); link.setTargetName("_new"); forumLinks.addComponent(link); } } if (!fragment.getKbRefs().isEmpty()) { kbRefs = new Panel("Pro Account Knowledge Base Articles"); kbRefs.setSizeUndefined(); for (Iterator<SourceFragment.Ref> iter = fragment.getKbRefs().iterator(); iter.hasNext(); ) { SourceFragment.Ref ref = iter.next(); String url = "http://vaadin.com/knowledge-base#" + ref.ref; Link link = new Link(ref.caption, new ExternalResource(url)); link.setTargetName("_new"); forumLinks.addComponent(link); } } } } // Show associated CSS if (example.getCssFragments() != null && example.getCssFragments().size() > 0) { SourceFragment csscode = example.getCssFragments().get(0); String srcurl = "http://dev.vaadin.com/browser/doc/book-examples/trunk/WebContent/VAADIN/themes/book-examples/styles.css"; horizontalOrder.addComponent(new SourceListing("CSS Code", srcurl, csscode)); } if (horizontalOrder.getComponentIterator().hasNext()) viewpanel.addComponent(horizontalOrder); if (bookRefs != null) viewpanel.addComponent(bookRefs); if (forumLinks != null) viewpanel.addComponent(forumLinks); if (kbRefs != null) viewpanel.addComponent(kbRefs); urifu.setFragment(example.getExampleId()); } } } }); Tree.ItemStyleGenerator itemStyleGenerator = new Tree.ItemStyleGenerator() { private static final long serialVersionUID = -3231268865512947125L; public String getStyle(Object itemId) { // Chapter title items do not contain a period if (!((String) itemId).contains(".")) return "chaptertitle"; return null; } }; menu.setItemStyleGenerator(itemStyleGenerator); }