/*
* Copyright 2008-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package editor;
import griffon.core.artifact.GriffonController;
import griffon.inject.MVCMember;
import griffon.metadata.ArtifactProviderFor;
import org.codehaus.griffon.runtime.core.artifact.AbstractGriffonController;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Map;
import static org.apache.commons.io.FileUtils.readFileToString;
import static org.apache.commons.io.FileUtils.writeStringToFile;
@ArtifactProviderFor(GriffonController.class)
public class EditorController extends AbstractGriffonController {
@MVCMember @Nonnull
private EditorModel model;
@MVCMember @Nonnull
private EditorView view;
@Override
public void mvcGroupInit(@Nonnull Map<String, Object> args) {
model.setDocument((Document) args.get("document"));
runOutsideUI(() -> {
try {
final String content = readFileToString(model.getDocument().getFile());
runInsideUIAsync(() -> model.getDocument().setContents(content));
} catch (IOException e) {
getLog().warn("Can't open file", e);
}
});
}
public void saveFile() {
try {
writeStringToFile(model.getDocument().getFile(), view.getEditor().getText());
runInsideUIAsync(() -> model.getDocument().setContents(view.getEditor().getText()));
} catch (IOException e) {
getLog().warn("Can't save file", e);
}
}
public void closeFile() {
destroyMVCGroup(getMvcGroup().getMvcId());
}
}