<commit_before>/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * 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.
 */

/*
 * Created by IntelliJ IDEA.
 * User: cdr
 * Date: Jul 15, 2007
 * Time: 4:04:39 PM
 */
package com.intellij.openapi.vfs.encoding;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileFilter;
import com.intellij.util.ui.tree.AbstractFileTreeTable;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.nio.charset.Charset;

class EncodingFileTreeTable extends AbstractFileTreeTable<Charset> {
  public EncodingFileTreeTable(@NotNull Project project) {
    super(project, Charset.class, "Default Encoding", VirtualFileFilter.ALL, false);
    reset(EncodingProjectManager.getInstance(project).getAllMappings());

    getValueColumn().setCellRenderer(new DefaultTableCellRenderer(){
      @Override
      public Component getTableCellRendererComponent(final JTable table, final Object value,
                                                     final boolean isSelected, final boolean hasFocus, final int row, final int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        final Charset t = (Charset)value;
        final Object userObject = table.getModel().getValueAt(row, 0);
        final VirtualFile file = userObject instanceof VirtualFile ? (VirtualFile)userObject : null;
        Pair<Charset, String> check = file == null || file.isDirectory() ? null : ChooseFileEncodingAction.checkCanReload(file);
        String failReason = check == null ? null : check.second;
        boolean enabled = failReason == null;

        // show existing encoding only if it was specified explicitly or it is unchangeable (with reason)
        boolean toShow = t != null || failReason != null;

        if (toShow) {
          Charset existing = check == null ? null : check.first;
          String encodingText = t != null ? t.displayName() : existing == null ? "N/A" : existing.displayName();
          setText(encodingText + (failReason == null ? "" : " (" + failReason + ")"));
        }

        setEnabled(enabled);
        return this;
      }
    });

    getValueColumn().setCellEditor(new DefaultCellEditor(new JComboBox()){
      private VirtualFile myVirtualFile;
      {<commit_msg>IDEA-100052 File Encodings tree has lines in UI<commit_after>/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * 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.
 */

/*
 * Created by IntelliJ IDEA.
 * User: cdr
 * Date: Jul 15, 2007
 * Time: 4:04:39 PM
 */
package com.intellij.openapi.vfs.encoding;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileFilter;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.tree.AbstractFileTreeTable;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.nio.charset.Charset;

class EncodingFileTreeTable extends AbstractFileTreeTable<Charset> {
  public EncodingFileTreeTable(@NotNull Project project) {
    super(project, Charset.class, "Default Encoding", VirtualFileFilter.ALL, false);
    reset(EncodingProjectManager.getInstance(project).getAllMappings());
    if (!UIUtil.isUnderGTKLookAndFeel()) {
      getTree().putClientProperty("JTree.lineStyle", "None");
    }
    getValueColumn().setCellRenderer(new DefaultTableCellRenderer(){
      @Override
      public Component getTableCellRendererComponent(final JTable table, final Object value,
                                                     final boolean isSelected, final boolean hasFocus, final int row, final int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        final Charset t = (Charset)value;
        final Object userObject = table.getModel().getValueAt(row, 0);
        final VirtualFile file = userObject instanceof VirtualFile ? (VirtualFile)userObject : null;
        Pair<Charset, String> check = file == null || file.isDirectory() ? null : ChooseFileEncodingAction.checkCanReload(file);
        String failReason = check == null ? null : check.second;
        boolean enabled = failReason == null;

        // show existing encoding only if it was specified explicitly or it is unchangeable (with reason)
        boolean toShow = t != null || failReason != null;

        if (toShow) {
          Charset existing = check == null ? null : check.first;
          String encodingText = t != null ? t.displayName() : existing == null ? "N/A" : existing.displayName();
          setText(encodingText + (failReason == null ? "" : " (" + failReason + ")"));
        }

        setEnabled(enabled);
        return this;
      }
    });

    getValueColumn().setCellEditor(new DefaultCellEditor(new JComboBox()){
      private VirtualFile myVirtualFile;
      {
{'fraction_non_alphanumeric': 0.10033343437405275, 'fraction_numerical': 0.008184298272203698, 'mean_word_length': 3.6636042402826856, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5110701', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1772, 'n_words': 529}