Example usage for javax.swing Box.Filler addComponentListener

List of usage examples for javax.swing Box.Filler addComponentListener

Introduction

In this page you can find the example usage for javax.swing Box.Filler addComponentListener.

Prototype

public synchronized void addComponentListener(ComponentListener l) 

Source Link

Document

Adds the specified component listener to receive component events from this component.

Usage

From source file:org.datavyu.controllers.component.MixerController.java

private void initView() {

    // Set default scale values
    minStart = 0;/* www .  j  a v  a 2 s  . c  o m*/

    listenerList = new EventListenerList();

    // Set up the root panel
    tracksPanel = new JPanel();
    tracksPanel.setLayout(new MigLayout("ins 0", "[left|left|left|left]rel push[right|right]", ""));
    tracksPanel.setBackground(Color.WHITE);

    if (Platform.isMac()) {
        osxGestureListener.register(tracksPanel);
    }

    // Menu buttons
    lockToggle = new JToggleButton("Lock all");
    lockToggle.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            lockToggleHandler(e);
        }
    });
    lockToggle.setName("lockToggleButton");

    bookmarkButton = new JButton("Add Bookmark");
    bookmarkButton.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            addBookmarkHandler();
        }
    });
    bookmarkButton.setEnabled(false);
    bookmarkButton.setName("bookmarkButton");

    JButton snapRegion = new JButton("Snap Region");
    snapRegion.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            snapRegionHandler(e);
        }
    });
    snapRegion.setName("snapRegionButton");

    JButton clearRegion = new JButton("Clear Region");
    clearRegion.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            clearRegionHandler(e);
        }
    });
    clearRegion.setName("clearRegionButton");

    zoomSlide = new JSlider(JSlider.HORIZONTAL, 1, 1000, 1);
    zoomSlide.addChangeListener(new ChangeListener() {
        public void stateChanged(final ChangeEvent e) {

            if (!isUpdatingZoomSlide && zoomSlide.getValueIsAdjusting()) {

                try {
                    isUpdatingZoomSlide = true;
                    zoomSetting = (double) (zoomSlide.getValue() - zoomSlide.getMinimum())
                            / (zoomSlide.getMaximum() - zoomSlide.getMinimum() + 1);
                    viewportModel.setViewportZoom(zoomSetting,
                            needleController.getNeedleModel().getCurrentTime());
                } finally {
                    isUpdatingZoomSlide = false;
                }
            }
        }
    });
    zoomSlide.setName("zoomSlider");
    zoomSlide.setBackground(tracksPanel.getBackground());

    JButton zoomRegionButton = new JButton("", zoomIcon);
    zoomRegionButton.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            zoomToRegion(e);
        }
    });
    zoomRegionButton.setName("zoomRegionButton");

    tracksPanel.add(lockToggle);
    tracksPanel.add(bookmarkButton);
    tracksPanel.add(snapRegion);
    tracksPanel.add(clearRegion);
    tracksPanel.add(zoomRegionButton);
    tracksPanel.add(zoomSlide, "wrap");

    timescaleController = new TimescaleController(mixerModel);
    timescaleController.addTimescaleEventListener(this);
    needleController = new NeedleController(this, mixerModel);
    regionController = new RegionController(mixerModel);
    tracksEditorController = new TracksEditorController(this, mixerModel);

    needleController.setTimescaleTransitionHeight(
            timescaleController.getTimescaleModel().getZoomWindowToTrackTransitionHeight());
    needleController
            .setZoomIndicatorHeight(timescaleController.getTimescaleModel().getZoomWindowIndicatorHeight());

    // Set up the layered pane
    layeredPane = new JLayeredPane();
    layeredPane.setLayout(new MigLayout("fillx, ins 0"));

    final int layeredPaneHeight = 272;
    final int timescaleViewHeight = timescaleController.getTimescaleModel().getHeight();

    final int needleHeadHeight = (int) Math.ceil(NeedleConstants.NEEDLE_HEAD_HEIGHT);
    final int tracksScrollPaneY = needleHeadHeight + 1;
    final int timescaleViewY = layeredPaneHeight - MixerConstants.HSCROLL_HEIGHT - timescaleViewHeight;
    final int tracksScrollPaneHeight = timescaleViewY - tracksScrollPaneY;
    final int tracksScrollBarY = timescaleViewY + timescaleViewHeight;
    final int needleAndRegionMarkerHeight = (timescaleViewY + timescaleViewHeight
            - timescaleController.getTimescaleModel().getZoomWindowIndicatorHeight()
            - timescaleController.getTimescaleModel().getZoomWindowToTrackTransitionHeight() + 1);

    // Set up filler component responsible for horizontal resizing of the
    // layout.
    {

        // Null args; let layout manager handle sizes.
        Box.Filler filler = new Filler(null, null, null);
        filler.setName("Filler");
        filler.addComponentListener(new SizeHandler());

        Map<String, String> constraints = Maps.newHashMap();
        constraints.put("wmin", Integer.toString(MixerConstants.MIXER_MIN_WIDTH));

        // TODO Could probably use this same component to handle vertical
        // resizing...
        String template = "id filler, h 0!, grow 100 0, wmin ${wmin}, cell 0 0 ";
        StrSubstitutor sub = new StrSubstitutor(constraints);

        layeredPane.setLayer(filler, MixerConstants.FILLER_ZORDER);
        layeredPane.add(filler, sub.replace(template), MixerConstants.FILLER_ZORDER);
    }

    // Set up the timescale layout
    {
        JComponent timescaleView = timescaleController.getView();

        Map<String, String> constraints = Maps.newHashMap();
        constraints.put("x", Integer.toString(TimescaleConstants.XPOS_ABS));
        constraints.put("y", Integer.toString(timescaleViewY));

        // Calculate padding from the right
        int rightPad = (int) (RegionConstants.RMARKER_WIDTH + MixerConstants.VSCROLL_WIDTH
                + MixerConstants.R_EDGE_PAD);
        constraints.put("x2", "(filler.w-" + rightPad + ")");
        constraints.put("y2", "(tscale.y+${height})");
        constraints.put("height", Integer.toString(timescaleViewHeight));

        String template = "id tscale, pos ${x} ${y} ${x2} ${y2}";
        StrSubstitutor sub = new StrSubstitutor(constraints);

        // Must call setLayer first.
        layeredPane.setLayer(timescaleView, MixerConstants.TIMESCALE_ZORDER);
        layeredPane.add(timescaleView, sub.replace(template), MixerConstants.TIMESCALE_ZORDER);
    }

    // Set up the scroll pane's layout.
    {
        tracksScrollPane = new JScrollPane(tracksEditorController.getView());
        tracksScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        tracksScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        tracksScrollPane.setBorder(BorderFactory.createEmptyBorder());
        tracksScrollPane.setName("jScrollPane");

        Map<String, String> constraints = Maps.newHashMap();
        constraints.put("x", "0");
        constraints.put("y", Integer.toString(tracksScrollPaneY));
        constraints.put("x2", "(filler.w-" + MixerConstants.R_EDGE_PAD + ")");
        constraints.put("height", Integer.toString(tracksScrollPaneHeight));

        String template = "pos ${x} ${y} ${x2} n, h ${height}!";
        StrSubstitutor sub = new StrSubstitutor(constraints);

        layeredPane.setLayer(tracksScrollPane, MixerConstants.TRACKS_ZORDER);
        layeredPane.add(tracksScrollPane, sub.replace(template), MixerConstants.TRACKS_ZORDER);
    }

    // Create the region markers and set up the layout.
    {
        JComponent regionView = regionController.getView();

        Map<String, String> constraints = Maps.newHashMap();

        int x = (int) (TrackConstants.HEADER_WIDTH - RegionConstants.RMARKER_WIDTH);
        constraints.put("x", Integer.toString(x));
        constraints.put("y", "0");

        // Padding from the right
        int rightPad = MixerConstants.R_EDGE_PAD + MixerConstants.VSCROLL_WIDTH - 2;

        constraints.put("x2", "(filler.w-" + rightPad + ")");
        constraints.put("height", Integer.toString(needleAndRegionMarkerHeight));

        String template = "pos ${x} ${y} ${x2} n, h ${height}::";
        StrSubstitutor sub = new StrSubstitutor(constraints);

        layeredPane.setLayer(regionView, MixerConstants.REGION_ZORDER);
        layeredPane.add(regionView, sub.replace(template), MixerConstants.REGION_ZORDER);
    }

    // Set up the timing needle's layout
    {
        JComponent needleView = needleController.getView();

        Map<String, String> constraints = Maps.newHashMap();

        int x = (int) (TrackConstants.HEADER_WIDTH - NeedleConstants.NEEDLE_HEAD_WIDTH
                + NeedleConstants.NEEDLE_WIDTH);
        constraints.put("x", Integer.toString(x));
        constraints.put("y", "0");

        // Padding from the right
        int rightPad = MixerConstants.R_EDGE_PAD + MixerConstants.VSCROLL_WIDTH - 1;

        constraints.put("x2", "(filler.w-" + rightPad + ")");
        constraints.put("height",
                Integer.toString(needleAndRegionMarkerHeight
                        + timescaleController.getTimescaleModel().getZoomWindowToTrackTransitionHeight()
                        + timescaleController.getTimescaleModel().getZoomWindowIndicatorHeight() - 1));

        String template = "pos ${x} ${y} ${x2} n, h ${height}::";
        StrSubstitutor sub = new StrSubstitutor(constraints);

        layeredPane.setLayer(needleView, MixerConstants.NEEDLE_ZORDER);
        layeredPane.add(needleView, sub.replace(template), MixerConstants.NEEDLE_ZORDER);
    }

    // Set up the snap marker's layout
    {
        JComponent markerView = tracksEditorController.getMarkerView();

        Map<String, String> constraints = Maps.newHashMap();
        constraints.put("x", Integer.toString(TimescaleConstants.XPOS_ABS));
        constraints.put("y", Integer.toString(needleHeadHeight + 1));

        // Padding from the right
        int rightPad = MixerConstants.R_EDGE_PAD + MixerConstants.VSCROLL_WIDTH - 1;

        constraints.put("x2", "(filler.w-" + rightPad + ")");
        constraints.put("height", Integer.toString(needleAndRegionMarkerHeight - needleHeadHeight - 1));

        String template = "pos ${x} ${y} ${x2} n, h ${height}::";
        StrSubstitutor sub = new StrSubstitutor(constraints);

        layeredPane.setLayer(markerView, MixerConstants.MARKER_ZORDER);
        layeredPane.add(markerView, sub.replace(template), MixerConstants.MARKER_ZORDER);
    }

    // Set up the tracks horizontal scroll bar
    {
        tracksScrollBar = new JScrollBar(Adjustable.HORIZONTAL);
        tracksScrollBar.setValues(0, TRACKS_SCROLL_BAR_RANGE, 0, TRACKS_SCROLL_BAR_RANGE);
        tracksScrollBar.setUnitIncrement(TRACKS_SCROLL_BAR_RANGE / 20);
        tracksScrollBar.setBlockIncrement(TRACKS_SCROLL_BAR_RANGE / 2);
        tracksScrollBar.addAdjustmentListener(this);
        tracksScrollBar.setValueIsAdjusting(false);
        tracksScrollBar.setVisible(false);
        tracksScrollBar.setName("horizontalScrollBar");

        Map<String, String> constraints = Maps.newHashMap();
        constraints.put("x", Integer.toString(TimescaleConstants.XPOS_ABS));
        constraints.put("y", Integer.toString(tracksScrollBarY));

        int rightPad = (int) (RegionConstants.RMARKER_WIDTH + MixerConstants.VSCROLL_WIDTH
                + MixerConstants.R_EDGE_PAD);
        constraints.put("x2", "(filler.w-" + rightPad + ")");
        constraints.put("height", Integer.toString(MixerConstants.HSCROLL_HEIGHT));

        String template = "pos ${x} ${y} ${x2} n, h ${height}::";
        StrSubstitutor sub = new StrSubstitutor(constraints);

        layeredPane.setLayer(tracksScrollBar, MixerConstants.TRACKS_SB_ZORDER);
        layeredPane.add(tracksScrollBar, sub.replace(template), MixerConstants.TRACKS_SB_ZORDER);
    }

    {
        Map<String, String> constraints = Maps.newHashMap();
        constraints.put("span", "6");
        constraints.put("width", Integer.toString(MixerConstants.MIXER_MIN_WIDTH));
        constraints.put("height", Integer.toString(layeredPaneHeight));

        String template = "growx, span ${span}, w ${width}::, h ${height}::, wrap";
        StrSubstitutor sub = new StrSubstitutor(constraints);

        tracksPanel.add(layeredPane, sub.replace(template));
    }

    tracksPanel.validate();
}