X-Git-Url: https://ruin.nu/git/?p=moosique.git;a=blobdiff_plain;f=MooView.java;h=9bf83673048412345f35a1f5d812ae1c1c825a56;hp=1cdbee8c36d7462569420d62aa640c9724a6b4d7;hb=HEAD;hpb=d7666fadd2f8baca8a03cacae836f2563fe4dd5d diff --git a/MooView.java b/MooView.java index 1cdbee8..9bf8367 100644 --- a/MooView.java +++ b/MooView.java @@ -1,18 +1,159 @@ +import javax.sound.midi.*; import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.beans.*; /** + * The main view, the container of the track views. * - * - * @author Andersson, Andreen, Lanneskog, Pehrson - * @version 1 + * @author Einar Pehrson */ - -public class MooView { + +public class MooView extends JScrollPane { + + private JPanel trackPanel; + private JPanel titlePanel; + private MooViewCounter viewCounter; + private JDialog progressDialog; + private JProgressBar progressBar; + private int numberOfTracks; + + /** + * Creates the main view + */ + public MooView(Track[] tracks) { + super(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + + // Configures scroll pane viewport (track views). + trackPanel = new JPanel(new GridLayout(1,3), true); + setViewportView(trackPanel); + getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE); + + // Configures viewport column header (track titles). + titlePanel = new JPanel(new GridLayout(1,3),true); + JViewport columnHeader = new JViewport(); + columnHeader.setView(titlePanel); + setColumnHeaderView(columnHeader); + + // Configures viewport row header (view counter). + viewCounter = new MooViewCounter(null); + JViewport rowHeader = new JViewport(); + rowHeader.setView(viewCounter); + setRowHeaderView(rowHeader); + + // Updates the view with the given tracks. + setTracks(tracks, false); + } + + /** + * Fills the track panel with track views for all tracks in the current sequence. + * @param tracks the tracks for which to add views + */ + public void setTracks(Track[] tracks, boolean showProgress) { + setDoubleBuffered(true); + numberOfTracks = tracks.length; + trackPanel.removeAll(); + titlePanel.removeAll(); + if (numberOfTracks == 1) { + // If MIDI file is of type 0, creates a view for the track. + trackPanel.setLayout(new FlowLayout()); + MooTrackTitle title = new MooTrackTitle(tracks[0]); + titlePanel.add(title); + MooTrackView mtv = new MooTrackView(tracks[0], title); + trackPanel.add(mtv); + title.setTrackView(mtv); + } else { + if (showProgress) { + // Creates dialog for progress bar. + progressDialog = new JDialog(Moosique.getGUI(), "Visualizing...", false); + int tracksToDraw = 0; + for (int i = 0; i < tracks.length; i++) { + if (Moosique.shouldBeDrawn(tracks[i])) tracksToDraw++; + } + progressBar = new JProgressBar(0, tracksToDraw); + progressBar.setValue(0); + progressBar.setStringPainted(true); + progressDialog.getContentPane().add(progressBar); + progressDialog.pack(); + progressDialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - progressDialog.getWidth()) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - progressDialog.getHeight()) / 2); + progressDialog.setVisible(true); + } + + // Starts filling the track panel with track views, while updating the progress bar. + GridLayout gL = new GridLayout(1,numberOfTracks); + trackPanel.setLayout(gL); + for (int i = 1; i < tracks.length; i++) { + if (Moosique.shouldBeDrawn(tracks[i])) { + MooTrackTitle title = new MooTrackTitle(tracks[i]); + titlePanel.add(title); + MooTrackView mtv = new MooTrackView(tracks[i], title); + trackPanel.add(mtv); + title.setTrackView(mtv); + if (showProgress) progressBar.setValue(i); + else System.out.print("."); + } else { + gL.setColumns(--numberOfTracks); + trackPanel.setLayout(gL); + } + } + if (showProgress) progressDialog.dispose(); + } +/* JPanel filler = new JPanel(); + int totalViewLength = trackPanel.getComponents().length * MooTrackView.VIEW_WIDTH; + if (totalViewLength < getWidth()) { + System.out.println("Adding filler since width = " + getWidth() + " and tracks = " + totalViewLength); + ((GridLayout)trackPanel.getLayout()).setColumns(numberOfTracks + 1); + filler.setPreferredSize(new Dimension(getWidth() - totalViewLength, 140 * MooTrackView.NOTE_HEIGHT)); + trackPanel.add(filler); + setPreferredSize(new Dimension(getWidth(), getHeight())); + } +*/ + trackPanel.validate(); + setViewportView(trackPanel); + } + + /** + * Calls on each track view to update itself. + */ + public void update(long tickPosition) { + getViewport().setViewPosition(new Point((int)getViewport().getViewPosition().getX(), (int)(tickPosition / (Moosique.getSequence().getResolution() / 4)) * MooTrackView.NOTE_HEIGHT)); + } + /** - * Creates + * Creates a view for the given track and adds it to the main view. + * @param track the track for which to find the view */ - public MooView () { + public MooTrackView getTrackView(Track track) { + for (int i = 0; i < trackPanel.getComponents().length; i++) { + MooTrackView mtv = (MooTrackView)(trackPanel.getComponents())[i]; + if(mtv.getTrack() == track) return mtv; + } + return null; + } + /** + * Creates a view for the given track and adds it to the main view. + * @param track the track for which to add a view + * @param index the index at which to insert the view + */ + public void addTrackView(Track track, int index) { + ((GridLayout)trackPanel.getLayout()).setColumns(++numberOfTracks); + ((GridLayout)titlePanel.getLayout()).setColumns(++numberOfTracks); + MooTrackTitle title = new MooTrackTitle(track); + titlePanel.add(title, index); + trackPanel.add(new MooTrackView(track, title), index); + validate(); + } + + /** + * Removes the view for the given track. + * @param index the index of the track for which to remove the view + */ + public void removeTrackView(int index) { + remove(index); + ((GridLayout)trackPanel.getLayout()).setColumns(--numberOfTracks); + validate(); } }