package jetbrains.mps.editor.runtime;
/*Generated by MPS */
import javax.swing.JComponent;
import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.mps.openapi.language.SProperty;
import jetbrains.mps.openapi.editor.EditorContext;
import jetbrains.mps.project.AbstractModule;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import jetbrains.mps.util.MacrosFactory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.mps.openapi.model.SNodeAccessUtil;
import java.io.File;
import javax.swing.JButton;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import java.awt.Component;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import jetbrains.mps.ide.ui.filechoosers.treefilechooser.TreeFileChooser;
import jetbrains.mps.vfs.FileSystem;
import jetbrains.mps.baseLanguage.closures.runtime.Wrappers;
import jetbrains.mps.vfs.IFile;
import org.jetbrains.mps.openapi.module.ModelAccess;
import org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.vfs.IFileUtils;
import java.awt.Dimension;
import jetbrains.mps.util.MacroHelper;
public class EditorUtil {
public EditorUtil() {
}
public static JComponent createSelectIconButton(final SNode sourceNode, final SProperty property, final EditorContext context, boolean copy) {
final AbstractModule module = (AbstractModule) sourceNode.getModel().getModule();
return createSelectButton(sourceNode, property, context, true, new _FunctionTypes._return_P1_E0<String, String>() {
public String invoke(String fullPath) {
return check_3m4h3r_a0a4a2a3(MacrosFactory.forModule(module), fullPath);
}
}, new _FunctionTypes._return_P1_E0<String, String>() {
public String invoke(String shortPath) {
return check_3m4h3r_a0a5a2a3(MacrosFactory.forModule(module), shortPath);
}
}, copy);
}
public static JComponent createSelectButton(final SNode sourceNode, final SProperty property, final EditorContext context, final boolean files, @NotNull final _FunctionTypes._return_P1_E0<? extends String, ? super String> shrinkPath, @NotNull _FunctionTypes._return_P1_E0<? extends String, ? super String> expandPath) {
return createSelectButton(sourceNode, property, context, files, shrinkPath, expandPath, false);
}
public static JComponent createSelectButton(final SNode sourceNode, final SProperty property, final EditorContext context, final boolean files, @NotNull final _FunctionTypes._return_P1_E0<? extends String, ? super String> shrinkPath, @NotNull _FunctionTypes._return_P1_E0<? extends String, ? super String> expandPath, final boolean copy) {
String filename = expandPath.invoke(SNodeAccessUtil.getProperty(sourceNode, property));
final File baseFile = (filename == null ? null : new File(filename));
final JButton button = new JButton();
button.setAction(new AbstractAction("...") {
@Override
public void actionPerformed(ActionEvent e) {
Component root = SwingUtilities.getRoot(button);
JFrame frame = (root instanceof JFrame ? (JFrame) root : null);
TreeFileChooser chooser = new TreeFileChooser();
chooser.setMode((files ? TreeFileChooser.MODE_FILES : TreeFileChooser.MODE_DIRECTORIES));
if (baseFile != null && baseFile.exists()) {
chooser.setInitialFile(FileSystem.getInstance().getFileByPath(baseFile.getAbsolutePath()));
}
final Wrappers._T<IFile> result = new Wrappers._T<IFile>(chooser.showDialog(frame));
if (result.value == null) {
return;
}
ModelAccess modelAccess = context.getRepository().getModelAccess();
if (copy) {
modelAccess.runWriteAction(new Runnable() {
public void run() {
SModel model = sourceNode.getModel();
String outputRoot = ((AbstractModule) model.getModule()).getModuleSourceDir().getPath() + File.separator + "icons";
IFile outputRootFile = FileSystem.getInstance().getFileByPath(outputRoot);
// copy
String source = MacrosFactory.forModule((AbstractModule) model.getModule()).expandPath(result.value.getPath());
IFile sourceFile = FileSystem.getInstance().getFileByPath(source);
IFileUtils.copyFileContent(sourceFile, outputRootFile.getDescendant(sourceFile.getName()));
result.value = outputRootFile.getDescendant(sourceFile.getName());
}
});
}
final String pathToShow = shrinkPath.invoke(result.value.getPath());
modelAccess.executeCommand(new Runnable() {
@Override
public void run() {
SNodeAccessUtil.setProperty(sourceNode, property, pathToShow);
}
});
}
});
button.setPreferredSize(new Dimension(20, 20));
return button;
}
private static String check_3m4h3r_a0a4a2a3(MacroHelper checkedDotOperand, String fullPath) {
if (null != checkedDotOperand) {
return checkedDotOperand.shrinkPath(fullPath);
}
return null;
}
private static String check_3m4h3r_a0a5a2a3(MacroHelper checkedDotOperand, String shortPath) {
if (null != checkedDotOperand) {
return checkedDotOperand.expandPath(shortPath);
}
return null;
}
}