private void setupSwatchTab(TabPanel panel) {
    final ListView<FlatColor> colorList = new ListView<FlatColor>();
    colorList.setModel(manager.colorManager.getSwatchModel());
    colorList.setColumnWidth(20);
    colorList.setRowHeight(20);
    colorList.setOrientation(ListView.Orientation.HorizontalWrap);
    colorList.setRenderer(
        new ListView.ItemRenderer<FlatColor>() {
          public void draw(
              GFX gfx,
              ListView listView,
              FlatColor flatColor,
              int i,
              double x,
              double y,
              double w,
              double h) {
            gfx.setPaint(flatColor);
            gfx.fillRect(x, y, w, h);
          }
        });
    EventBus.getSystem()
        .addListener(
            colorList,
            SelectionEvent.Changed,
            new Callback<SelectionEvent>() {
              public void call(SelectionEvent e) throws Exception {
                int n = e.getView().getSelectedIndex();
                setSelectedFill(colorList.getModel().get(n));
                popup.setVisible(false);
              }
            });
    final PopupMenuButton switcher = new PopupMenuButton();
    final ArrayListModel<Palette> palettes = manager.colorManager.getPalettes();
    switcher.setModel(palettes);

    Button addButton = new Button("+");
    addButton.onClicked(
        new Callback<ActionEvent>() {
          public void call(ActionEvent actionEvent) throws Exception {
            if (!palettes.get(switcher.getSelectedIndex()).isEditable()) {
              return;
            }
            final Stage dialog = Stage.createStage();
            dialog.setTitle("Color");

            final ColorPickerPanel picker = new ColorPickerPanel();

            Callback<ActionEvent> okay =
                new Callback<ActionEvent>() {
                  public void call(ActionEvent event) {
                    FlatColor color = picker.getColor();
                    manager.colorManager.addSwatch(color);
                    dialog.hide();
                  }
                };
            Callback<ActionEvent> canceled =
                new Callback<ActionEvent>() {
                  public void call(ActionEvent event) {
                    dialog.hide();
                  }
                };
            dialog.setContent(
                new VFlexBox()
                    .add(picker)
                    .add(
                        new HFlexBox()
                            .add(new Button("okay").onClicked(okay))
                            .add(new Button("cancel").onClicked(canceled))));
            dialog.setWidth(400);
            dialog.setHeight(370);
            dialog.centerOnScreen();
          }
        });

    switcher.setTextRenderer(
        new ListView.TextRenderer() {
          public String toString(SelectableControl selectableControl, Object palette, int i) {
            if (palette instanceof Palette) {
              return ((Palette) palette).getName();
            } else {
              return "foo";
            }
          }
        });

    EventBus.getSystem()
        .addListener(
            switcher,
            SelectionEvent.Changed,
            new Callback<SelectionEvent>() {
              public void call(SelectionEvent selectionEvent) throws Exception {
                int n = selectionEvent.getView().getSelectedIndex();
                manager.colorManager.setCurrentPalette(palettes.get(n));
                colorList.setModel(manager.colorManager.getSwatchModel());
              }
            });

    VFlexBox vbox = new VFlexBox();
    vbox.setFill(FlatColor.GRAY);
    vbox.add(colorList, 1);
    vbox.add(new HFlexBox().add(addButton).add(switcher));
    vbox.setBoxAlign(FlexBox.Align.Stretch);
    panel.add("Swatches", vbox);
  }
  private void setupPatternTab(TabPanel panel) throws IOException {
    double size = 40;

    final ListView<Paint> patternList =
        new ListView<Paint>()
            .setModel(manager.patternManager.getModel())
            .setColumnWidth(size)
            .setRowHeight(size)
            .setOrientation(ListView.Orientation.HorizontalWrap)
            .setRenderer(paintItemRenderer);
    EventBus.getSystem()
        .addListener(
            patternList,
            SelectionEvent.Changed,
            new Callback<SelectionEvent>() {
              public void call(SelectionEvent e) throws Exception {
                int n = e.getView().getSelectedIndex();
                setSelectedFill(patternList.getModel().get(n));
                popup.setVisible(false);
              }
            });
    Button addButton = new Button("add image");
    addButton.onClicked(
        new Callback<ActionEvent>() {
          public void call(ActionEvent actionEvent) throws Exception {
            FileDialog fd = new FileDialog((Frame) null);
            fd.setMode(FileDialog.LOAD);
            fd.setTitle("Open Pattern Image");
            fd.setVisible(true);
            if (fd.getFile() != null) {
              File file = new File(fd.getDirectory(), fd.getFile());
              u.p("opening a file" + file);
              try {
                PatternPaint pat = PatternPaint.create(file);
                manager.patternManager.addPattern(pat);
              } catch (IOException ex) {
                ex.printStackTrace();
              }
            }
          }
        });
    Button createButton = new Button("create new");
    createButton.onClicked(
        new Callback<ActionEvent>() {
          public void call(ActionEvent event) throws Exception {
            final PatternBuilder builder = new PatternBuilder();
            final Stage stage = Stage.createStage();
            Callback<ActionEvent> closeAction =
                new Callback<ActionEvent>() {
                  public void call(ActionEvent event) throws Exception {
                    PatternPaint pattern = builder.getPattern();
                    manager.patternManager.addPattern(pattern);
                    stage.hide();
                  }
                };
            Callback<ActionEvent> cancelAction =
                new Callback<ActionEvent>() {
                  public void call(ActionEvent event) throws Exception {
                    stage.hide();
                  }
                };
            stage.setContent(
                new VFlexBox()
                    .add(builder, 1)
                    .add(
                        new HFlexBox()
                            .add(new Button("cancel").onClicked(cancelAction), 0)
                            .add(new Button("save").onClicked(closeAction), 0),
                        0));
            stage.setWidth(600);
            stage.setHeight(350);
            stage.centerOnScreen();
          }
        });
    VFlexBox vbox = new VFlexBox();
    vbox.setBoxAlign(FlexBox.Align.Stretch);
    vbox.setFill(FlatColor.GRAY);
    vbox.add(patternList, 1);
    vbox.add(new HFlexBox().add(addButton).add(createButton));
    panel.add("Patterns", vbox);
  }