⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 id3tagpage.java

📁 eclise rcp 项目,是非常好的学习源码
💻 JAVA
字号:
/*******************************************************************************
 * Copyright (c) 2007 Siemens AG
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Kai T鰀ter - initial API and implementation
 *******************************************************************************/

package com.siemens.ct.mp3m.ui.editors.id3;

import java.util.Vector;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import com.siemens.ct.mp3m.model.IMP3Info;


/**
 * An editor page for ID3 tags.
 * 
 * @author Kai Toedter
 * @author (Last changed by: $author$)
 * @version $Revision: 1.0 $
 */
public class Id3TagPage extends FormPage {
	public static final int SONG_INFO = 0;

	public static final int TECH_INFO = 1;

	private FormToolkit toolkit;

	private Composite section;

	private int info;

	private IMP3Info id3TagInfo;

	private boolean isDirty = false;

	private Vector<StringFieldEditor> fieldEditors = new Vector<StringFieldEditor>();

	/**
	 * Creates a new Id3TagPage object.
	 * 
	 * @param editor
	 *            the editor
	 * @param id
	 *            the editor's id
	 * @param title
	 *            the editor's title!
	 * @param path
	 * @param id3TagInfo
	 * @param info
	 */
	public Id3TagPage(Id3Editor editor, String id, String title, String path, IMP3Info id3TagInfo, int info) {
		super(editor, id, title);
		this.info = info;
		this.id3TagInfo = id3TagInfo;
	}

    private void addKeyValuePair(String key, String value, boolean editable) {
		StringFieldEditor stringEditor = new StringFieldEditor(key,
				key + ":", section); //$NON-NLS-1$
		toolkit.adapt(stringEditor.getTextControl(section), true, true);
		stringEditor.setStringValue(value);
		stringEditor.setEnabled(editable, section);
		Label label = stringEditor.getLabelControl(section);
		label.setEnabled(true);
		toolkit.adapt(label, false, false);

		stringEditor.setPropertyChangeListener(new IPropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent event) {
				if (event.getProperty().equals("field_editor_value")) //$NON-NLS-1$
					isDirty = true;
				getEditor().editorDirtyStateChanged();
			}
		});
		fieldEditors.add(stringEditor);
	}


    protected void createFormContent(IManagedForm managedForm) {
        toolkit = managedForm.getToolkit();

        String mp3Path = ((Id3Editor) getEditor()).getFullPath();
        section = createSection(
                managedForm,
                Messages.getString("Id3TagPage.Tags_Section"), Messages.getString("Id3TagPage.For_filename") + mp3Path); //$NON-NLS-1$ //$NON-NLS-2$

        new Label(section, SWT.NONE);
        new Label(section, SWT.NONE);

        try {
            if (info == TECH_INFO) {
                addKeyValuePair(
                        Messages.getString("Id3TagPage.Size_Label"), Integer.toString(id3TagInfo.getSize()), false); //$NON-NLS-1$
                addKeyValuePair(
                        Messages.getString("Id3TagPage.Duration_Label"), Integer.toString(id3TagInfo.getDuration()), false); //$NON-NLS-1$
                addKeyValuePair(
                        Messages.getString("Id3TagPage.Bitrate"), id3TagInfo.getBitrate(), false); //$NON-NLS-1$
                addKeyValuePair(
                        Messages.getString("Id3TagPage.Frequency"), id3TagInfo.getFrequency(), false); //$NON-NLS-1$
            } else {
                addKeyValuePair(
                        Messages.getString("Id3TagPage.Title_Label"), id3TagInfo.getTitle(), true); //$NON-NLS-1$
                addKeyValuePair(
                        Messages.getString("Id3TagPage.Artist_Label"), id3TagInfo.getArtist(), true); //$NON-NLS-1$
                addKeyValuePair(
                        Messages.getString("Id3TagPage.Album_Label"), id3TagInfo.getAlbum(), true); //$NON-NLS-1$
                addKeyValuePair(
                        Messages.getString("Id3TagPage.Track_Label"), id3TagInfo.getTrack(), true); //$NON-NLS-1$
                addKeyValuePair(
                        Messages.getString("Id3TagPage.Year_Label"), id3TagInfo.getYear(), true); //$NON-NLS-1$
                addKeyValuePair(
                        Messages.getString("Id3TagPage.Genre_Label"), id3TagInfo.getGenre(), true); //$NON-NLS-1$
                addKeyValuePair(
                        Messages.getString("Id3TagPage.Comment_Label"), id3TagInfo.getComment(), true); //$NON-NLS-1$
                Composite composite = toolkit.createComposite(section);
                composite.setLayout(new RowLayout());
                GridData data = new GridData();
                data.horizontalSpan = 2;
                composite.setLayoutData(data);
            }
        } catch (Exception e) {
            System.out.println("View.createFormContent: " + e); //$NON-NLS-1$
        }
    }

	private Composite createSection(IManagedForm managedForm, String title,
			String desc) {
		FormToolkit toolkit = managedForm.getToolkit();

		Composite formBody = managedForm.getForm().getBody();
		formBody.setLayoutData(new GridData(GridData.FILL_BOTH));
		formBody.setLayout(new GridLayout(1, false));

		int style = Section.TITLE_BAR | Section.CLIENT_INDENT
				| Section.EXPANDED | Section.DESCRIPTION;
		Section section = toolkit.createSection(formBody, style);
		section.setLayoutData(new GridData(GridData.FILL_BOTH));
		section.setLayout(new GridLayout(1, false));

		section.setText(title);
		section.setDescription(desc);
		toolkit.createCompositeSeparator(section);

		Composite composite = toolkit.createComposite(section);
		section.setClient(composite);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		composite.setLayout(new GridLayout());
		return composite;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.editor.FormPage#isDirty()
	 */
	@Override
	public boolean isDirty() {
		return isDirty;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.editor.FormPage#doSave(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void doSave(IProgressMonitor monitor) {
		writeFieldsToTag();
		id3TagInfo.saveMP3File();
		isDirty = false;
	}

	private void writeFieldsToTag() {
		for (StringFieldEditor stringEditor : fieldEditors) {
			if (stringEditor.getLabelText().equals(
					Messages.getString("Id3TagPage.Title_Label") + ":")) { //$NON-NLS-1$ //$NON-NLS-2$
				id3TagInfo.setTitle(stringEditor.getStringValue());
			} else if (stringEditor.getLabelText().equals(
					Messages.getString("Id3TagPage.Album_Label") + ":")) { //$NON-NLS-1$ //$NON-NLS-2$
				id3TagInfo.setAlbum(stringEditor.getStringValue());
			} else if (stringEditor.getLabelText().equals(
					Messages.getString("Id3TagPage.Artist_Label") + ":")) { //$NON-NLS-1$ //$NON-NLS-2$
				id3TagInfo.setArtist(stringEditor.getStringValue());
			} else if (stringEditor.getLabelText().equals(
					Messages.getString("Id3TagPage.Track_Label") + ":")) { //$NON-NLS-1$ //$NON-NLS-2$
				id3TagInfo.setTrack(stringEditor.getStringValue());
			} else if (stringEditor.getLabelText().equals(
					Messages.getString("Id3TagPage.Year_Label") + ":")) { //$NON-NLS-1$ //$NON-NLS-2$
				id3TagInfo.setYear(stringEditor.getStringValue());
			} else if (stringEditor.getLabelText().equals(
					Messages.getString("Id3TagPage.Comment_Label") + ":")) { //$NON-NLS-1$ //$NON-NLS-2$
				id3TagInfo.setComment(stringEditor.getStringValue());
			} else if (stringEditor.getLabelText().equals(
					Messages.getString("Id3TagPage.Genre_Label") + ":")) { //$NON-NLS-1$ //$NON-NLS-2$
				id3TagInfo.setGenre(stringEditor.getStringValue());
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -