Package com.life.audiotageditor.handlers

Source Code of com.life.audiotageditor.handlers.OpenFileHandler

/**
* This file is part of ${project_name}.
*
* ${project_name} is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* ${project_name} is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* ${project_name}. If not, see <http://www.gnu.org/licenses/>.
*
* @see http://www.gnu.org/licenses/lgpl.txt
* @author art <lijianghui2000@gmail.com>
*/
package com.life.audiotageditor.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import com.life.audiotageditor.constants.Constants;
import com.life.audiotageditor.model.AudioFile;
import com.life.audiotageditor.model.AudioModel;
import com.life.audiotageditor.model.AudioModelManager;
import com.life.audiotageditor.utils.StringUtil;
import com.life.audiotageditor.views.AudioView;

public class OpenFileHandler extends AbstractHandler {
  /**
   * The constructor.
   */
  public OpenFileHandler() {
  }

  /**
   * the command has been executed, so extract extract the needed information
   * from the application context.
   */
  public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow window = HandlerUtil
        .getActiveWorkbenchWindowChecked(event);
    String path = openFileDialog(window);
    if (path == null || path.isEmpty()) {
      return null;
    }
    AudioView audioView = (AudioView) window.getActivePage().findView(
        AudioView.ID);
    AudioModel audioModel = (AudioModel) AudioModelManager.instance()
        .getRoot();
    AudioFile audioFile = (AudioFile) audioModel.getFile(StringUtil
        .formatPath(path));
    audioView.getTreeViewer().setInput(audioFile);
    audioView.getTreeViewer().setSelection(
        new StructuredSelection(audioFile), true);
    return null;
  }

  private String openFileDialog(IWorkbenchWindow window) {
    FileDialog dialog = new FileDialog(window.getShell(), SWT.OPEN);
    dialog.setText(Messages.OpenFileHandler_dialog_text);
    dialog.setFilterExtensions(Constants.FILE_TYPE.split("[,]")); //$NON-NLS-1$ //$NON-NLS-2$
    return dialog.open();
  }
}
TOP

Related Classes of com.life.audiotageditor.handlers.OpenFileHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.